Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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.

share|improve this answer
    
Another option which may be preferable in some scenarios is set "pwd=abc&123" –  Harry Johnston Feb 28 '13 at 2:57

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.

share|improve this answer

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.

share|improve this answer
    
+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. –  Harry Johnston Mar 10 '13 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 Mar 10 '13 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. –  Harry Johnston Mar 10 '13 at 19:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.