Pass special characters from input to bash script

I have a bash script that just needs to add a new user and sign the password that is passed when the script is called:

./adduser_script username password

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

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

The problem arises, of course, when the password contains special characters, such as $ @ , $ * itd. Therefore, when I call the script:

/adduser_script username aa$@bbb

and after the script, the completion of the password looks like this: aabbb (so special characters are removed from the original password). The question is, how to correctly pass the original password using special characters in the script?

Thanks in advance, Relations

+4
source share
4 answers

, , script , , . , , , .

, script, - $a, bash a , .

$ , .

./adduser_script username 'password$@aaa'
+1

qoutes??

'aa $@bb' qoutes, .. "aa $@bb"

: check with echo command

echo "aa$@bb" will print aabb

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

script

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

qoutes .

+3

escape. : "MyComplexP\@\$\ $word"

0
/usr/sbin/useradd ... -p "$(openssl passwd -1 '$2')"
-1

Source: https://habr.com/ru/post/1541397/


All Articles