shell programming - terminal input

created onApril 18, 2022

read

User input from the terminal can be processed with the command By default read reads user input from the terminal until the user hits return and stores the input in the variable . The input of the user is echoed on the screen unless read is invoked with the option -s for secret (or secure).

general syntax of read:

options

option description
-p <string> preceed the input prompt with the string argument
-n <num> read only num characters from the user input
-s do not echo user input on screen

example

Read username and password and store them in the variables username and password. For the username, only up to 8 characters are allowed (for strings longer than 8 characters, all characters after the eighth characater will be silently discarded). The input for the password is not printed on the terminal:

read -n 8 -p "your username: " username read -sp "your password: " password