Silent reading



Linux Command Line and Shell Scripting Bible
Published by Wiley Publishing, Inc.


There are times when you need input from the script user, but you don’t want that input to display on the monitor. The classic example of this is when entering passwords, but there are plenty of other types of data that you will need to hide.

The -s option prevents the data entered in the read command from being displayed on the monitor (actually, the data is displayed, but the read command sets the text color to the same as the background color). Here’s an example of using the -s option in a script:

$ cat test27
#!/bin/bash
# hiding input data from the monitor
read -s -p "Enter your password: " pass
echo
echo "Is your password really $pass?"
$ ./test27
Enter your password:
Is your password really T3st1ng?
$

The data typed at the input prompt doesn’t appear on the monitor but is assigned to the variable just fine for use in the script.

Comentários