How to prompt user for input in shell script / Read command in linux with examples

Read user input in shell script
Getting user input is important for creating interactive and dynamic shell scripts so that they execute as user wants. For example, a file reader shell script which reads the file location from the user or a menu based script which performs actions based on user input.

Linux and unix systems provide read command that can be used to capture user input and store it in variable. Syntax for reading user input in a shell script is given below

read message

Above command will wait for the user to enter characters and will keep on reading till enter key(or new line) is pressed. User input will be stored in a variable named message.
If you want to prompt a message to the user asking to provide input, then use -p option as

read -p “Enter a message ” message
echo “$message”

This will print a message on the terminal asking the user to enter some input and will store the input in message variable. Next it will print the value stored in message variable.
Output of above script will be

$ ./input.sh
Enter a message Hello there!!
Hello there!!

If you want to display a message without using -p option, then you need to use echo command to first display the message and then read the user input as shown below

echo “Enter a message”
read message

read command has many other options which are explained below but if you want to simply read an input from user, you can now do that.
For more advanced read command tutorial, continue this article.

linux read command
As stated above, read command is a builtin command on linux and unix systems to read input. It can be used to read from standard input(keyboard) or from a file also.
It has many options which make it very powerful and flexible and this post will explain all those with examples.
Read with prompt
Display a message asking for user to enter input. Example,

read -p “Enter your age: ” age
echo “Age is” $age “years”

Output will be

Enter your age 15
Age is 15 years


Read a password

Use read command with -s option standing for silent. It will not display the characters entered by the user. Example,

read -p “Enter password ” -s password
echo “Password is ” $password

Notice the use of multiple options in the same read command. Also, -p option should be immediately followed by the prompt to be displayed.
Output of above code will be

Enter password
Password is

Note that the characters entered by the user are not displayed on the terminal.
Split entered string on some character
Suppose you want to read some key-value pairs from the user in key=value format and extract key and value separately from the input, use -a option of read to split the input and assign it to array. Array elements will start at 0 index. Example,

IFS=”=”
# split input on “=” and assign each token to the array
read -a array -p “Enter key-value: ” input
echo “Key is ${array[0]}
echo “Value is ${array[1]}

where IFS is Field separator on which read command will split the input. Here, we set it to “=” since we need to break the input on this character.

Output of above code is

Enter key-value: website=codippa
Key is website
Value is codippa

Remember that it will not work if you do not set IFS to the character on which the input should be split.
Reading only fixed number of characters
Sometimes you want to limit the size of input from the user, such as when asking for a permission Y/N, we want the user to enter only 1 character.
Rather than applying a check on the size of input, read command provides -n option that will automatically terminate reading after the specified number of characters. Example,

# read only 1 character
read
-n 1 “Would you like to continue (y/n)? ” continue
# check for valid input character
if [[ !($continue== ‘y’) && !( $continue== ‘n’ ) ]];then
   echo -e “\nInvalid option”
else
   echo -e “\nOk..”
fi

Output will be

Would you like to continue (y/n)? t
Invalid option
Would you like to continue (y/n)? y
Ok..

read command will automatically terminate after you enter the first character, you don’t even need to press enter after the specified number of characters have been read.
Reading from a file
read command can also be used to read from a file directly by using file descriptors. Its -u option followed by a file descriptor performs the read operation. Example,

# assign a file descriptor to a file
exec 4< ./data.txt
# read from the file descriptor and assign it to variable
read -u 4 line
# display variable value
echo $line

Below table summarizes all the above read command options along with some additional options for quick reference.

OptionDescriptionExample
-aStores the words in an array variable which follows -a.
Default separator is a space but you can change it by setting IFS value.
read -a arr
-dSpecifies the delimiter till which read should keep on accepting input.
Default delimiter is newline or till enter is pressed.
read -d “q” input
Will keep on reading till q is pressed.
input is a variable where user input will be stored
-e
-iThis option followed by some text is used as an initial input(or some default value) from the user.
This option can only be used with -e option and when -s option is not used.
User can change the initial text.
read -e -i “My message is ” input

Will display “My message is ” and then wait for user input

-nThis option is followed by an integer which denotes the maximum number of  characters that the command will read or till enter(or end of line) is pressed, whichever comes earlier.read -n 20 input

Will read max 20 characters or less than 20 if enter is pressed earlier and store it in input variable.

-NThis option is similar to -n but it will ignore the newline character(or enter key) and will keep on reading till the number of characters have been read or timeout specified with -t option is reached.read -N 20 input

Will read max 20 characters and store it in input variable.

-pUsed for displaying a message to the user.
This option is followed by a string which is displayed before the user can enter his input.
read -p “Enter your name ” name

Displays the above message and waits for user input.
User input will be stored in name variable.

-rStands for raw input. Will read backslashes(\) as text rather than escape characters.read -p “Enter a message ” -r info

Displays “Enter a message” prompt and waits for user input.
User input will be stored in info variable.

-sStands for silent input. It will hide the characters input by the user.
Used for taking password or secret inputs from the user.
read -p “Enter password” -s code
-tRepresents a timeout. Followed by an integer which is the number of seconds that the command should wait for the user to input a value.read -t 10 input

Wait for 10 seconds for user to enter characters.

-uSpecifies the file descriptor from where read should accept input.
Default is standard input(or keyboard).
read -u 4 line

Read a line from the file denoted by a file descriptor

As you can see, read command is a simple to use but it becomes very powerful with all its options.
That is all on read command in linux. Do click the clap button below if you liked the article.

Leave a Reply