#!/bin/bash
read -p "Please Enter you OS Password:" PASSWORD
echo "the password is $PASSWORD"
输入:Test$1234
我们希望输出为Test\$1234
答案 0 :(得分:1)
您可以使用printf "%q" $PASSWORD
“shell-escape”$PASSWORD
的内容。这不仅会处理$
,还会处理其他特殊的shell字符,例如!
,&
,>
等。
#!/bin/bash
read -p "Please Enter you OS Password:" PASSWORD
PASSWORD=$(printf "%q" $PASSWORD)
echo "the password is $PASSWORD"
答案 1 :(得分:0)
您可以尝试使用以下命令,此处sed
命令会将$
替换为\$
,并且不会替换任何其他特殊字符,因此无副作用: -
!/bin/bash
read -p "Please Enter you OS Password:" PASSWORD
PASSWORD=$(echo $PASSWORD |sed 's/\$/\\$/g')
echo "the password is $PASSWORD"