top of page

Read User Input in Linux Shell Script

When you make shell scripts interactive, it gives a personal touch for the end user. Accepting users input with shell scripts makes users feel in control.

User Input with Read Command

Use Linux read command to print a message and store user input into a variable. The below command prints (-p) a message and waits for user input. Anything that users types and hits enter, gets stored in fname variable

read -p "Enter First Name: " fname
echo "First Name is $fname"

The above command will infinitely wait for user to enter an input. You can timeout read command after few seconds with -t option

read -t 10 -p "Enter Last Name: " lname

To read passwords from user without showing it on screen

read -s -p "Password: " password
echo "Your password is $password"

Linux Read Command With Echo

You can use echo command to print message on screen and then use read command

echo "Enter Full Name: "
read fullname
echo "Full Name is $fullname"

My preferred method is the first one as read command allows you to print (-p) and read user input into a variable in one single command!


Related Posts

Heading 2

Add paragraph text. Click “Edit Text” to customize this theme across your site. You can update and reuse text themes.

bottom of page