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 command output to the variable

Different types of bash commands need to be run from the terminal based on the user’s requirements. When the user runs...
29/12/2020

Bash Echo Examples

Multiple built-in functions exist in bash to print the output into the terminal. ‘echo’ is one of the most used...
29/12/2020

Associative array in Bash

An array variable is used to store multiple data with index and the value of each array element is accessed by the corresponding...
29/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024