
The Bash read command is a built-in command for capturing user input. The command reads text from the standard input and assigns the value to one or more variables. You can combine the bash read command with conditional statements and loops to validate user input, read file contents, and perform specific tasks in a script.
This article explains how to use the Bash read command. You'll use the command to create variables, read files, and validate user input in a shell or Bash script environment.
The following is a basic read command syntax in Bash.
In the above command:
[options]: Includes optional flags that modify the command's behavior.var_name: Includes the variable name that stores the input data.The read command supports the following options:
-p: Displays a prompt before capturing the user input.-s: Enables silent mode and hides input characters.-n: Limits the number of input characters.-t: Times out the input prompt after a specific number of seconds if the command receives no input.-a: Stores the input into an array.-r: Prevent backslashes (\) from being interpreted by the shell as escape or special characters,The read command also supports multiple input values. You can store these values using separate variables with the following syntax.
To explore more on handling multiple conditions in Bash scripts, check out our guide on the bash case statement for effective pattern matching and control flow.
Follow the steps below to use the read command to capture user input in a Bash shell environment.
Capture user input using the What's your name prompt and store the value in a name variable.
Enter John Doe when prompted and press Enter to store the input in the name variable.
Print the variable using the echo command.
Output:
Run the following read command to store the user input in multiple variables, such as var1, var2, and var3.
Enter a string, such as Hello World! This_Works separated by spaces when prompted.
Print the value of each variable.
Output:
The bash read command assigns any extra values to the last variable when input values exceed the available variables. For example, with an input value such as Hello World! This_works in bash, the script assigns This_works in bash value to the last var3 variable.
The read command works with STDIN (Standard Input) by default. You can redirect other commands' outputs to the read command for processing and storage in a variable. This approach is important especially when working with files. Follow the steps below to use the read command with redirections.
Create a new greet variable using the read command and a Hello World! Greetings from Vultr string <<< redirection.
Print the greet variable.
Output:
Create a new greeting variable and redirect its value to the read command.
Output:
Create a new readme.txt file to use with the read command.
Add the following contents to the readme.txt file.
Save and close the file.
Display the contents of the readme.txt file using the read command and a while loop.
The above while loop uses the read command to display all lines in the readme.txt input file. The loop increments the i variable using the ((i++)) statement to number each line in the output.
Output:
IFS (Internal Field Separator) is a built-in Bash variable that splits characters into separate strings or fields. IFS enables the read command to split multiple inputs using spaces, tabs, and newlines delimiters by default. In the following steps, use IFS to enable other characters as delimiters to split user input with the read command.
IFS uses the following inline syntax with the read command unless you specify a delimiter in the global variable.
Input a new string, such as Hello World! This_works separated by spaces using the read command to split and store the values in multiple variables.
By default, IFS uses spaces as the default delimiter for the read command.
Print the value of each variable.
Output:
Set a new IFS delimiter, such as : to modify the read command's split method.
Input the Hello World! This_works string again.
Print the value of the first variable. This time, the read command assigns the text to the first variable because you changed the delimiter.
Output:
Input the Hello:World:This_works string again and separate the string using the : delimiter.
Print the value of the first variable again.
Output:
Print the value of each variable.
Output:
Use IFS and the read command with multiple delimiters such as :, _, and - to split the input.
Output:
Reset IFS to the default delimiter settings.
The bash read command stores multiple values into arrays using the -a option and splits the input using the default delimiter. Follow the steps below to use the read command with arrays in Bash.
Input a new Hello World! This Works string value and store the input in a new_array array.
Print all elements in the array.
Output:
Create a new del_array array and set : as the delimiter using IFS to split the input characters.
Print all elements in the array.
Output:
The read command captures and assigns user inputs to variables. You can use these variables to perform specific tasks like validating user input using conditional statements by following the steps below.
Create a new validate.sh script.
Add the following contents to the validate.sh file.
Save and close the file.
The above script compares the user input from the name variable using the if-else conditional statement. If the condition returns true and the name matches any element in the admins array, the script executes the following commands.
Run the script using Bash.
Enter a valid user, such as user1 when prompted.
Output:
You have used the bash read command to capture and validate user input. You have also combined the read command with loops, conditional expressions, and arrays to perform specific tasks. The read command is important when automating Bash scripts that require user input to perform specific actions on a system.
0 Comments
Be the first to comment and share your perspective with the community.