I'm getting some troubles to use a password with special characters such as $ in a bash shell script.
My shell script is :
read -s -p "Password : " bindDNPass
ldapadd -H ldap://localhost -x -w $bindDNPass -D "dn=cn=Admin" -f /tmp/file.ldif
And the password could be something like $Something18$.
Well, the command
ldapadd -H ldap://localhost -x -W -D "dn=cn=Admin" -f /tmp/file.ldif`
asks for my $Something18$
, and works fine.
But if I try
ldapadd -H ldap://localhost -x -w $Something18$ -D "dn=cn=Admin" -f /tmp/file.ldif
it doesn't work. I guess it's trying to resolve the variable $Something18,
so I tried with \$Something18$
, \$Something18\$,
\\\$Something18$
, ... but it keeps on failing...
How can I do? (Without changing my password...)
I see two potential problems with how you're reading and using the password:
read
command without the-r
option, it'll try to interpret escape (backslash) sequences, which may cause trouble.Fixing these potential problems gives this script snippet:
...But, while you should do both of these mods to make your script more robust, neither of these will change how it handles the password
$Something18$
. In fact, when I tried your original snippet with that password, it got passed toldapadd
correctly. If your actual password has some other special characters in it (or you've played with the value ofIFS
), these might help; otherwise, there's something else going on.If your password still doesn't work after these fixes, try putting
set -x
before theldapadd
command (andset +x
after) so it'll print what's actually being passed toldapadd
. Well, it'll print it in a possibly confusing form: it'll print an equivalent command to what's actually being executed, which means it'll add quotes and/or escapes to the password parameter as necessary so that you could run that command and it'll do the same thing. When I tried it with$Something18$
, it printed:...where the single-quotes mean that what's inside them is passed directly, with no parsing. It could also have printed any of the following equivalent commands:
so you have to take what it prints, and figure out how that'd be parsed by bash, in order to figure out what's actually being passed to
ldapadd
. But at least it'll give you some information about what's actually happening.Oh, and you may notice that the DN argument isn't being double-quoted. That's because it doesn't contain any special characters, so the double-quotes aren't doing anything, so it just left them off.
Put it in double-quotes and escape the
$
symbol avoid special interpretation from theshell
,(or) [more recommended]
Enclose it within single-quote to let the shell treat it as a literal string without expanding it,
From the
man bash
page,