13

I am trying something like:

set pwd = abc&123
echo password %pwd%

I get the result as...

'123' is not recognized as an internal or external command, operable program or batch file. 
password abc

Is the system like this or is there an escape for such characters?

1

5 Answers 5

12

What you need to do is wrap your password in double quotes like so:

set pwd="abc&123"
echo %pwd%

Let me know how it goes.

3
  • 1
    Another option which may be preferable in some scenarios is set "pwd=abc&123" Commented Feb 28, 2013 at 2:57
  • 1
    -1 it is shocking that this has had 5 upvotes, it's wrong. Windows uses %var% or !var! you could've tested it yourself so easily. $var is in linux, if you echo $var in windows it just says literally $var
    – barlop
    Commented May 12, 2015 at 11:25
  • 2
    @barlop: seeing as it's the other line that addresses the question, I don't see that it matters. Commented May 18, 2015 at 1:42
10

I found that it works best with a combination of both martineau's answer and mastashake57's answer.

set pwd=abc^&123
echo %pwd%

Still fails, and

set pwd="abc&123"
echo %pwd%

Adds quotes to it (you can't remove the quotes with a for loop or string manipultaion because it will fail again, thanks to the ampersand), which isn't that great.

However,

set "pwd=abc^&123"
echo %pwd%

Works perfectly.

Hope that helps.

6
  • +1. Be aware that this leaves the carat in the environment variable, i.e., it sets pwd to abc^&123. Most of the time this is probably exactly what you need to do, but it could catch you out if you're not aware of what's happening. Commented Mar 10, 2013 at 10:27
  • @HarryJohnston I must correct your spelling error. It's carEt. btw I know ntcmds.chm mentions it, but where in cmd /? documentation is it mentioned about the caret being the escape character?
    – barlop
    Commented Mar 10, 2013 at 11:52
  • @Barlop: so it is; I was confusing it with the homonym. As a general rule /? only provides a summary, not comprehensive documentation. Commented Mar 10, 2013 at 19:59
  • @barlop, ? The caret is the escape character.
    – Pacerier
    Commented Aug 25, 2015 at 17:23
  • @Pacerier yes, caret is the escape character , nobody is disagreeing with that. and it is mentioned that it is the escape character, in ntcmds.chm(ntcmds.chm came with XP and is in an equivalent online microsoft.com/resources/documentation/windows/xp/all/proddocs/… and maybe in some ways more up to date online - maybe more commands). I was just asking where in /? (if anywhere), it says that caret is the escape character
    – barlop
    Commented Aug 25, 2015 at 18:09
7

From the command-line reference help file:

The ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.

Seems like putting the whole password in quotation marks would be easier than escaping individual characters if there's more than two characters within it that need the treatment.

1
  • And quotes within quotes?
    – Pacerier
    Commented Aug 25, 2015 at 17:17
5

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
2
0

I needed a solution for cases where the location of the & character is unknown, this seems to work for me:

set "pwd=abc&123"
set "EchoablePassword=%pwd:&=^&%"
echo password %EchoablePassword%

Not all commands requires the escape of the & character!!! For example the "if exist" command won't work if you escape the & character, but will work if you don't escape it. This is why I named the variable as "EchoablePassword" because in my code only echo requires the escape of "&".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .