How should i pass the password(containing special chars) as commandline argument?

batch-file, command-line-arguments, special-characters, windows

Solution

You can't escape your password in any way. As some combinations with quotes together with spaces can't be placed into one parameter.

Like this `"` (`<space><quote><space>`) even if you add some quotes around, it's not possible, even if you quote the spaces and the quotes itself.

myBat  " 
myBat " " "
myBat ^ " 
myBat ^"^ ^"^ ^"

Here is the best to double all quotes inside your password and enclose your password into quotes. Then you can use all other characters are without any problems.

deployment.bat " foo\$ser\""ver\ 1 "

And in deployment.bat

@echo off
setlocal DisableDelayedExpansion
set "pwd=%~1"
setlocal EnableDelayedExpansion
set "pwd=!pwd:""="!"
echo pwd='!pwd!'

You should use your password only with delayed expansion to avoid problems with special characterts.

For accessing command line parameters you could also look at SO: How to receive even the strangest command line parameters?

Problem

I have a deployment script to which i have to pass LDAP password as cmd paramater. actual password: ` foo\$ser"ver\\ 1 ` (contains three space characters: at the beginning, before `1`, and after `1`) e.g ``` ...bin>deployment.bat LDAPPassword= foo\$ser\"ver\\ 1 ``` Note:There are spaces in the password as shown at the beginning. The `deployment.bat` calls a class to which the above parameter is passed as an argument. The problem is that the class receives 2 distinct arguments: ``` args[0]= foo\$ser"ver\\ //The space after \\ is omitted args[1]=1 //The space before and after 1 is omitted ``` How do I pass this password so that it is received as single string? I have already tried quoting the password as ``` ...bin>deployment.bat LDAPPassword=" foo\$ser"ver\\ 1 " ``` however it won't work.

Original source

Related problems