In Bash, one way to interact with users is by reading user input, which allows scripts and programs to accept data directly from the keyboard or other input sources. So, reading user input is a crucial aspect of creating interactive scripts. From this article, you will know how to read bash user inputs using the read command with some practical cases.
Syntax of “read” Command
In Bash, the read command is a built-in command used to read input from the user or from a specified input source. It then assigns the entered value to one or more variables for further script processing. Here’s a basic syntax of the read command:
read [OPTION]... Variable…
Options of “read” Command
Some useful options for the “read” command are provided below:
|Let the command perform in the ‘silent’ mode. Does not echo the user’s input.
|Returns after reading the specified number of characters.
|Returns after reading the specified number of characters while ignoring the delimiter.
|Reads line until the given delimiter instead of a new line.
|Sets the time in seconds that the script should wait for the input from the user.
|Disables backlashes to escape characters.
|Shows a message to the user before the input prompt.
|Assigns the given word sequence to a variable named array.
|Begins an interactive shell session to get the line to read.
|Adds initial text before a line is read as a prefix.
|-u (file descriptor)
|Reads from file descriptor instead of standard input.
5 Cases of Reading User Inputs Using the “read” Command
Bash uses the read command to read user inputs. In this article, I will discuss some practical cases of how to create bash scripts to read user inputs.
1. Read Single User Input Using Bash Script
In this case, I will show you how you can simply use the
read command to read a single user input. Follow the script to accomplish this task:
echo "What’s your name?"
echo "Hello, $name! Nice to meet you."
In the above image, you can see that, I successfully ran the created intro.sh script. The script shows a user interactive message What’s your name?. Later it has been read & displayed after the user has inserted the value ‘Joy’.
2. Read Multiple User Inputs Using Bash Script
The read command in Bash can read multiple variables at a time. For that, just provide a list of variable names separated by spaces, and the command will assign the corresponding inputs to each variable. Let’s see a bash script to read multiple user inputs using the
echo "Enter your name, age, and gender: "
read name age gender
echo "Name: $name"
echo "Age: $age"
echo "Gender: $gender"
In the above image, you can see that the script is showing a user interactive message for multiple inputs. Later these values have been read & displayed after the user has inserted the values joy 20 male (separated by space).
3. Read Input from a File in Bash
You can use the read command within a loop to process each line of a file. In this case, the script will simply echo each line with a processing message, but you can perform any desired operations. Here’s a bash script to see how the
read command works to read input from a file:
while IFS=read -r line
echo "Processing: $line"
# Performs actions on each line
done < "$filename"
In the above image, you can see that the script is reading & processing each line of the file input.txt. After reading, it is also displaying them one by one.
4. Read User Password Securely Using Bash
Using the read command with option
-s you can assign any sensitive user information as the command option hides them without being displayed on the screen. Here’s a bash script to read the user password securely using the
echo -n Enter your password:
read -s password
# Perform actions in silent mode
echo "Password entered: $password"
In the above image, you can see that the script is showing a user interactive message Enter your password. Later, the user enters the password but the command reads it in silent mode without displaying it. Which we can see after printing the value.
5. Create an Interactive Prompt for Multiple Input Types
You can also write a bash program to generate prompts for multiple inputs. In this case, the prompt will ask the user three different questions and print them at the end. Check the bash script below to create an interactive prompt for multiple input types:
echo "Welcome. What should we call you: "
echo "It’s nice to meet you" $var_name
read -p "Enter your age: " number
echo "So, you are a $number year old!"
echo "Do you like bash programming?(y/n)"
echo "That’s good to know, thank you."
First, it welcomed the user and asked for the user name, in the next line the user inserted the value jane. After that, the program prints “It’s nice to meet you jane”. Where jane is the variable value. Then asks for the user’s age by Entering your age and the user inserted 12. The next line prints the user’s age with the line “So, you are a 12 year old!”. Finally, asks a yes/ no question, and the user inserts Yes by entering the letter “y”. After reading the message “That’s good to know, thank you.” is displayed.
To sum up, learning to create interactive scripts is a mandatory Bash scripting task. And for that, you have to learn to read user inputs. Hope this article helps in attaining the necessary knowledge.
People Also Ask
- How to Use Input Argument in Bash [With 4 Practical Examples]
- How to Use Bash Input Parameter? [5 Practical Cases]
- How to Wait for User Input in Bash [With 4 Practical Examples]
- How to Read CSV Files in Bash? [2 Methods]