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.
Key Takeaways
- Learning about the ‘read’ command.
- Learning about practical cases of how to read user inputs.
Free Downloads
The ‘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.
Command Syntax >
read [OPTION]... Variable…
Useful Options
- -s → Let the command perform in the ‘silent’ mode.
- -n → Returns after reading the specified number of characters.
- -t → Sets the time in seconds that the script should wait for the input from the user.
- -r → Disables backlashes to escape characters.
- -p → Shows a message to the user before the input prompt.
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.
Case 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. Please follow the below steps to check the process:
➊ At first, launch an Ubuntu Terminal.
➋ Now, write the following command to open a file in the nano text editor:
nano intro.sh
➌ Afterward, write the following script in the nano editor:
#! /bin/bash
echo “What’s your name?”
read name
echo “Hello, $name! Nice to meet you.”
➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.
➎ After that, use the following command to make the script executable:
chmod u+x intro.sh
➏ Finally, run the script by the following command:
./intro.sh
In the above image, you can see that, I successfully ran the created “intro.sh” script. The script is showing a user interactive message ‘What’s your name?’. Later it has been read & displayed after the user has inserted the value ‘Joy’.
Case 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.
Script (info.sh) >
#! /bin/bash
echo “Enter your name, age, and gender: ”
read name age gender
echo “Name: $name”
echo “Age: $age”
echo “Gender: $gender”
Now, run the script by the following command:
./info.sh
In the above image, you can see that, I successfully ran the created “info.sh” script. 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).
Case 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.
Script (file.sh) >
#! /bin/bash
filename=”input.txt”
while IFS=read -r line
do
echo “Processing: $line”
# Performs actions on each line
done < “$filename”
First, assign the file name you want to read from to the variable ‘filename’. Here, the file name is set to ‘input.txt’. Make sure the file exists in the same directory as the script or provide the full path to the file. Then, the ‘while IFS=read -r line’ line initiates a ‘while’ loop that iterates over each line of the file. Where in ‘IFS=read -r line’, the ‘read’ command reads each line from the file and assigns it to the variable ‘line’. By setting ‘IFS= ’ and using the ‘-r’ option it is ensured that leading & trailing whitespaces are preserved, and backlashes are not interpreted as escape characters. And ‘do’ sets actions for the command. After that, ‘echo “Processing: $line”’, this line prints the current line being processed. Lastly, ‘done < “$filename”, this line redirects the contents of the file specified by the ‘$filename’ variable as input for the ‘while’ loop. Each line of the file will be read and processed until the end of the file is reached.’
Now, run the script by the following command:
./file.sh
In the above image, you can see that, I successfully ran the created “file.sh” script. The script is reading & processing each line of the file ‘input.txt’. After reading, it is also displaying them one by one.
Case 4: Read User Password Securely Using Bash
Using the ‘read’ command with option ‘–silent’ or ‘-s’ you can assign any sensitive user information as the command option hides them without being displayed on the screen.
Script (password.sh) >
#! /bin/bash
echo -n Enter your password:
read -s password
echo
# Perform actions in silent mode
echo “Password entered: $password”
Now, run the script by the following command:
./password.sh
In the above image, you can see that, I successfully ran the created “password.sh” script. 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.
Case 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.
Script (prompt.sh) >
#!/bin/bash
echo "Welcome. What should we call you: "
read var_name
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)”
read
echo “That’s good to know, thank you.”
Now, run the script by the following command:
./prompt.sh
Conclusion
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
Related Articles
- 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]
<< Go Back to Bash Input | Bash I/O | Bash Scripting Tutorial