views:

100

answers:

3
set A=2 && echo %A%

This does not echo A as 2 in windows. Is there any way to do it?

A=2 ; echo $A

works in bash. I want a similar behavior on windows

A: 

Use one & instead of 2.

The following command in a bat file:

set A=2 & echo %A%

prints nicely 2 as output

Patrick
It is not working. I think you are running it twice or you already have it set
Kamal
+1  A: 

I'm sure there are many ways to do this, here are two of them:

  • setlocal ENABLEDELAYEDEXPANSION&set "foo=bar baz"&echo.!foo!&endlocal
  • set "foo=bar baz"&for /F "tokens=1,* delims==" %%A in ('set foo') do if "%%~A"=="foo" echo.%%B

Edit: Added check to "filter" set results for 2nd solution, thanks Johannes Rössel

Anders
Note that for loops use %%X for variables in batch files, and just %X in the "terminal"
Anders
For the second variant, you should hope that there aren't more variables starting with `foo`.
Joey
The first one as it is prints !foo!
Kamal
@Kamal: You must put it in a batch file
Anders
A: 

Note the ! surrounding A instead of %.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET A=2 & ECHO !A!
ENDLOCAL
aphoria
I already posted code like that...
Anders
@Anders Really?
aphoria
It doesnot work for me.. it echoes !A!
Kamal
@aphoria yes really
Anders
@Kamal It worked on XP and Vista for me. Try copy/pasting into a .CMD file and then running in a new command window.
aphoria
@Anders I see you did post similar code. For such a small snippet of code, there are bound to be similarities.
aphoria