Harry
Harry

Reputation: 89

Why does a SET not take hold in an ECHO command in the same line?

I am new to Windows 10 64-bit and start Command Prompt and type the following (set variable and echo it in same line):

H:\>set a="hello" & echo %a%
%a%

H:\>set a="hello" & echo %a%
"hello"

Why do I not see "hello" the first time I echo'ed it?

Apologies if this a basic question, I cannot seem to have found the answer online.

Upvotes: 2

Views: 191

Answers (1)

Szabolcs Dombi
Szabolcs Dombi

Reputation: 5783

This is because the line will be evaluated before the set command, so on the first run %a% will be passed as is (a string "%a%" because a was not present).

After evaluating variables the first line will look like:

set a="hello" & echo %a%

The second line will be evaluated as:

set a="hello" & echo hello

before running any commands.

Upvotes: 1

Related Questions