6

When I type:

set hi=Hello^&World!
echo %hi%

it print Hello and tell me World is not a command

I want it prints Hello&World!

How to do this?

Celebi
  • 1,280
  • 3
  • 16
  • 25

3 Answers3

22

This works for me:

set "hi=Hello^&World!"
echo %hi%

Outputs

Hello&World!
Bali C
  • 30,582
  • 35
  • 123
  • 152
5

The only secure way to echo the content of a variable is to use the delayed expansion here.
If percent expansion is used, it depends on the content if it fails.

set "var1=Hello ^& World"
set "var2=Hello & World"
setlocal EnableDelayedExpansion
echo !var1!
echo !var2!
echo %var1%
echo %var2% -- fails

The delayed expansion is more usefull as it doesn't interpret any special characters.
More info at SO: How the parser works

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225
3

just

echo Hello ^& World!

works

EDIT so the problem is not with ECHO command, but with the assignment of the variable, as @Bali correctly pointed out.

PA.
  • 28,486
  • 9
  • 71
  • 95