Bash Scripting - User Input
Any programming language can perform the common task of receiving user input. In a bash script, there are various ways to receive user input. The bash script uses the read command to retrieve data from the user. By using the read command's various options, one or more data can be read from a bash script. This tutorial illustrates a few typical applications for the read command.

Option of Read Command
Option | Purpose |
-p | Before the input prompt, it is used to give the user a helpful message. |
-S | It is used to gather user input that cannot be seen. Use this choice to obtain a password or other sensitive information. Its name is silent mode. |
-n | It is used to set the limit of input characters. |
-t | It is used to set the amount of time, in seconds, to wait before receiving user input. |
Let's look at a simple example:
#!/bin/bash
#Questions:
echo "What is your name?:"
read name
echo "What is your last name?"
read last_name
#Print:
echo "Your full name is $name $last_name"
Output:
[root@scriptbox scripts]# clear
[root@scriptbox scripts]# vim user_input.sh
[root@scriptbox scripts]# chmod +x user_input.sh
[root@scriptbox scripts]# ./user_input.sh
What is your name?:
David
What is your last name?
Tzemach
Your full name is David Tzemach
Using read command with options
The -p option, which lets you specify a prompt, and the -s option, which turns on silent input, are the two most frequently used options. This can make it simple to request a username and password combination, such as the one in the example below:
#!/bin/bash
#Questions:
echo "Enter Credentials:"
read -p "Username: " name
read -sp "Password" pass
#Print:
echo "User name and pass is ok"
echo "Access Approved"
Output:
[root@scriptbox scripts]# ./user_input.sh
Enter Credentials:
Username: Dan
PasswordUser name and pass is ok
Access Approved
[root@scriptbox scripts]#
Using the read command while under a time restriction
To capture time-limited input from the user, create a bash file with the following script. Here, seconds will be used to measure time. The program in the example below will wait for the user's input for 10 seconds, and if the user is unable to type the data within 10 seconds, the program will exit without returning any information.
#!/bin/bash
#Questions:
echo "Enter Credentials:"
read -p "Username: " name
read -sp "Password:" pass
echo "You have 10s to approve cred"\n
read -t 10 -p "approve Cred" tmp
#Print:
echo "-------------------------"
echo "User name and pass is ok"
echo "Access Approved"
Output:
[root@scriptbox scripts]# ./user_input.sh
Enter Credentials:
Username: F
Password:You have 10s to approve credn
approve Cred-------------------------
User name and pass is ok
Access Approved
[root@scriptbox scripts]#
Use of read command with -n option
To accept input of a certain length, create a bash file with the following script. The user will be permitted to input a maximum of 5 characters, per the script.
#!/bin/bash
#Questions:
echo "Enter Credentials:"
# Take input of a maximum 5 characters long
read -p "Username:" -n 5 name
echo
read -sp "Password:" pass
echo "You have 10s to approve cred"\n
#Print:
echo "-------------------------"
echo "User name and pass is ok"
echo "Access Approved"