What you really need is:
set pwd=abc^^^&123
echo %pwd%
Or:
set "pwd=abc^&123"
echo %pwd%
Explanation:
Since &
is a special char, you must escape it with ^
, resulting in ^&
.
Running set pwd=abc^&123
will set the variable to the value abc&123
. After that, if you were to run echo %pwd%
, you are effectively running echo abc&123
.
Yet echo abc&123
doesn't work as you expect, because &
is a special char. You can avoid this by adding quotes: echo "abc&123"
but your output would have the quote chars too. In other words, using quotes isn't exactly a proper solution.
What you need is to escape the &
char when you run the echo
command:
echo abc^&123
So the variable needs to contain the value abc^&123
. To set the variable to the value abc^&123
, you would need to escape the special chars ^
and &
, resulting in:
set pwd=abc^^^&123