gaspar gaspar - 1 year ago 73
Bash Question

pass special characters from input to bash script

I've a bash script that simple has to add new user and sign a password that is passed when script is called:

./adduser_script username password

and the password is then used as a parameter in the script like this:

/usr/sbin/useradd ... -p `openssl passwd -1 "$2"` ...

the problem occurs of course when password contains special characters like $@, $* itd. So when i call the script:

/adduser_script username aa$@bbb

and after script ends password looks like: aabbb (so the special charakters are removed from original password). The question is how can I correctly pass the original password with special charakters to the script?

Thanks in advance,

Answer Source

have you tried strong qoutes ??

use 'aa$@bb' instead of weak qoutes i.e. "aa$@bb"

for example: check with echo command

echo "aa$@bb" will print aabb


echo 'aa$@bb' will print aa$@bb

In your script use

/usr/sbin/useradd ... -p `openssl passwd -1 '$2'` ...

now you need not to worry about qoutes while passing password as argument.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download