5

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,240
  • 3
  • 15
  • 24

3 Answers3

20

This works for me:

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

Outputs

Hello&World!
Bali C
  • 29,317
  • 35
  • 118
  • 150
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
  • 74,839
  • 15
  • 166
  • 215
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.
  • 27,165
  • 9
  • 70
  • 90