1. Overview

In Bash, we see whatever we type in the terminal. However, there might be cases where we don’t want this behavior. One case might be requesting a password from a user either in the terminal or in a Bash script.

In this tutorial, we’ll go over a few methods for hiding user input in the terminal or a Bash script:

  • The read command
  • The stty command
  • The Python Programming Language

2. Using the read Command

As its name implies, the read command reads a line from the terminal and splits it into fields:

$ read first_variable second_variable
aaa bbb
$ echo $first_variable
$ echo $second_variable

The read command, by default, uses whitespace characters as the separator. So, the shell variable first_variable got the value aaa and the shell variable second_variable got the value bbb.

As we see in the output of the read command, the input we typed, which was aaa bbb, was echoed in the terminal. What if we don’t want what we type to be echoed in the terminal? For example, we may want to ask for a password. We can use the -s option of the read command:

$ read -s -p "Please enter your password: " passwd
Please enter your password:

Here, what we typed as the password was not printed in the terminal. This is because of the -s option. The -s option does not echo the input entered to the standard output (the terminal in this case). The -p option prints the string following it before reading the input. In our example, it just printed the prompt, “Please enter your password: “. The passwd is the shell variable that will store the input, which in our case is the password that the user types. Assuming that we entered secret_password as the input, we now have got the password:

$ echo $passwd

3. Using the stty Command

The stty command displays or changes the settings of a terminal and is also useful for hiding user input.

We can use the echo setting of the stty command for enabling or disabling the echoing of input characters.

stty -echo disables the echoing – no input character will be displayed. On the other hand, stty echo enables the echoing, meaning that the input characters will be displayed in the terminal as usual. Let’s use this setting for getting a password in a Bash script, get_password.sh:

echo "Please enter your password: "
stty -echo
read passwd
stty echo
echo "The password is: $passwd"

Let’s run this script:

$ get_password.sh
Please enter your password: 
The password is: secret_password

If we have an already existing shell variable named as passwd in the calling terminal, changing the value of that shell variable in the Bash script doesn’t affect its value in the calling terminal:

$ passwd="my_password"
$ get_password.sh
Please enter your password:
The password is: secret_password
$ echo $passwd

If we want to change the value of the shell variable in the calling terminal, we should run the script by sourcing it:

$ passwd="my_password"
$ . get_password.sh
Please enter your password: 
The password is: secret_password
$ echo $passwd

We prefer using the stty command in a Bash script because if we had used it directly in the terminal, we wouldn’t have seen the commands we typed after the command stty -echo, either. The commands would be visible only after we typed the command stty echo.

4. Using the Python Programming Language

The Python programming language usually comes installed in Linux distributions. We can execute Python statements from the terminal without writing Python scripts:

$ user_password=$(python -c "import getpass; passwd=getpass.getpass(); print(passwd)")
$ echo $user_password

The –c option passed to the python command lets us execute Python statements from the terminal.

The first statement within the double quotes, import getpass, imports the getpass module. This module is a part of the Python Standard Library. We can liken Python modules to header files in the C programming language. A module is just a file containing Python definitions and statements.

We use the getpass() method of the getpass module as the second statement within the double quotes, passwd=getpass.getpass(). The getpass() method prompts the user password without echoing the typed input, which is exactly what we want. We then assign the password that the user types to the Python variable, passwd.

By default, the prompted string is “Password: “, but we can change it:

$ user_password=$(python -c "import getpass; passwd=getpass.getpass('Please enter your password: '); print(passwd)")
Please enter your password: 
$ echo $user_password 

As we see, we can specify the prompted string as a parameter to the getpass() method.

Finally, we print the Python variable, passwd, using the statement print(passwd). The output of the print statement gets assigned to the shell variable, user_password, because of the command substitution.

5. Conclusion

In this tutorial, we discussed several ways of hiding user input in Bash.

We saw that we can use the read command with its -s option to hide the user input. We can also use the stty command for disabling and enabling the echoing of user input. The getpass() method of the getpass module of the Python programming language can be another alternative.

Comments are closed on this article!