command line - Windows Batch echo variable with old value -


here's sample:

d:\>set var=123  d:\>set var=456 & echo %var% 123  d:\>set var=789 & echo %var% 456 

a new value set in var variable, echo still displays old value.

anyone have clue happened? , how correct value?

when execution of command or block of commands (commands concatenated or enclosed in parenthesis) requested, parser replaces read operations on variables content in variable before starting execute commands/block

in case, value in variable changed @ execution time, value echo console determined before change made. can tested with

set "var=abc" set "var=123" & set var & echo %var% 

set var show correct value in variable (in command there no variable read operation replaced parser) echo show old value (replaced parser before command execution).

inside batch files, usual way handle use delayed expansion (setlocal enabledelayedexpansion command), allow change, needed, syntax access variables %var% !var!, indicating parser read operation must delayed until command executed.

but can not enable delayed expansion command line. maybe enabled (not default state) , able use it

set "var=abc" set "var=123" & echo !var! 

but said, not usual case. need escape percent signs hide variable parser , force second parser evaluation of line.

set "var=abc" set "var=123" & call echo ^%var^% 

or can spawn separate cmd instance delayed expansion enabled

set "var=abc" set "var=123" & cmd /v /c"echo !var!" 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -