Bash Script User Input

28/12/2020
Taking input from the user is a common task for any programming language. You can take input from a user in bash script in multiple ways. A read command is used in the bash script to take data from the user.  Single or multiple data can be taken in bash script by applying different options of the read command. Some common uses of the read command are shown in this tutorial.

Example-1: Using simple read command

In this example, a single data is taken from the user and we print the value. After running the script, the program will wait for the user input. When the user types the data and presses enter then the data will be stored in answer variable. The value of answer variable is printed later. One thing, you should remember that you don’t need to use ‘$’ symbol at the time of assigning a variable’s value but you have to use ‘$’ symbol at the time of reading the variable.

#!/bin/bash
echo -n "What is your favorite food : "
read answer
echo "Oh! you like $answer!"

Output:

Example-2: Using read command with options

-p option is used with read command to display some helpful message for the user related to input. -s option is used to hide the text from the terminal which will be typed by the user. This is called silent mode and used for password data. The following example shows the use of both options.

#!/bin/bash
# Type your Login Information
read -p ‘Username: ‘ user
read -sp ‘Password: ‘ pass

if (( $user == "admin" && $pass == "12345" ))
then
     echo -e "nSuccessful login"
else
     echo -e "nUnsuccessful login"
fi

Output:

Example-3: Using read command to take multiple inputs

If you want to take multiple inputs at a time then you have to use read command with multiple variable names. In the following example, four inputs are taken in four variables by using read command.

#!/bin/bash

# Taking multiple inputs
echo "Type four names of your favorite programming languages"
read lan1 lan2 lan3 lan4
echo "$lan1 is your first choice"
echo "$lan2 is your second choice"
echo "$lan3 is your third choice"
echo "$lan4 is your fourth choice"

Output:

Example-4: Using read command with the time limit

If you want to set time restricted input for the user then you have to use -t option with a read command. Here, time is counted as second. In the following example, the program will wait for 5 seconds for user’s input and if the user is unable to type the data within 5 seconds then the program will exit without value.

#!/bin/bash
read -t 5 -p "Type your favorite color : " color
echo $color

Output:

So, you can retrieve input from the user in different ways using read command based on the requirement of your script.

For more information watch the video!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Comments

How to use single and multiple line comments in BASH Using comments in any script or code is very important to make the...
28/12/2020

Bash yes Command

Bash `yes` command is one of those commands of Linux that is related to the operation of another command. Using this command...
29/12/2020

Linux Screen Command Tutorial

In this lesson on Linux Screen Command, we will install and use various commands related to Screen command. The screen...
29/12/2020