I’m writing a bash script that will receive a password via STDIN and go through a few different checks.
I’m getting a problem when I use a password that contain symbols.
The plain password grassy is OK, but a complex combination such as gra$$y will expand to gra3308y.
This is the script I am using:
read INPUT
if [ $(echo -n $INPUT|wc -m) -ge 6 ]
then exit 0
else exit 1
fi
I’ve tried all different kinds of quotations but I can’t stop the STDIN password from expanding special characters.
Here is an example, without putting single quotes around the original value what can be done to solve this?
david@hostname ~ $ echo gra$$| { read INPUT; echo ${INPUT}; }
gra2598
Use double quotes around variable expansions inside your script:
While you’re developing a script, use
echoto show what you’re working with, but also use double quotes.If you type a line at this program, there will be no shell expansion done on the characters you type. If you simulate a user typing with
echo, then you need to prevent the shell that executes theechofrom expanding any metacharacters, thus:Omitting the single quotes means that the shell will replace the
$$with a PID (probably the PID of the parent shell rather than the shell in the pipeline, but with some PID). This is different from you typing 5 characters plus newline after running:You could also use:
The
<control-D>is the EOF indication; it flushes all zero characters typed since the last newline was entered, andcatinterprets zero bytes available as EOF. The file therefore contains 6 characters: the letters ‘g‘, ‘r‘, ‘a‘, two ‘$‘ signs and a newline. This will be read by your script verbatim; there will be no expansion on the data.