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

How to debug a bash script?

A script is just a file containing a set of commands. The basic idea of a script is to automatically execute and re-execute...
29/12/2020

Bash wait for keypress

`read` command is used to take user input in a bash script. We can take input in bash script by using various types of...
29/12/2020

30 bash script Interview questions and answers

Bash scripting is a very useful and powerful programming language that is mainly used to make any manual task automated....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024

Thuê mua proxy xoay ở đâu uy tín, chất lượng, giá tốt?
11/05/2024

Thuê mua proxy game nâng cao trải nghiệm trò chơi
10/05/2024

Thuê mua proxy Airdrop tốc độ nhanh, hiệu quả cực tốt
10/05/2024