Bash Read User Input
To read user input in Bash, use the read builtin command and store the input in one or more variables. The value can then be used later in the shell script.
In this tutorial, we shall learn how to read input provided by a user through the Bash shell terminal. We will begin with the basic read command, then cover prompts, multiple variables, full-line input, silent password input, arrays, default values, and simple validation.
Bash read command syntax for user input
read is a Bash builtin command that reads one line from standard input. In an interactive script, that standard input is usually the terminal keyboard. The simplest form is shown below.
read <variable_name>
For practical scripts, the syntax is often written with options and one or more variable names.
read [options] variable1 variable2
If no variable name is given, Bash stores the input in the special variable REPLY. In most scripts, it is clearer to name the variable explicitly.
Example: read a name from the Bash terminal
Following is an example Bash Script to read user input using read command.
Bash Script
#!/bin/bash
echo "Enter your name.."
read name
echo "Hello $name ! Learn to Read input from user in Bash Shell"
In this example, third line, echo command is used to prompt user for an input.
Fourth line, read command reads user input via keyboard, to a variable named name.
Fifth line, echo command uses name variable to display to the terminal.
Output
$ ./bash-readinput-example
Enter your name..
Arjun
Hello Arjun ! Learn to Read input from user in Bash Shell
Show the Bash input prompt with read -p
Instead of writing a separate echo statement before read, you can use the -p option. This keeps the prompt and the input-reading command on the same line.
#!/bin/bash
read -p "Enter your city: " city
echo "City: $city"
In interactive scripts, read -p is commonly used for short prompts such as names, file paths, menu choices, and confirmation questions.
Read input to multiple Bash variables in one read command
You may also read input to multiple variables in a single read command.
In the following example, we shall read firstname followed by lastname in a single read command.
Bash Script
#!/bin/bash
echo "Enter your FirstName LastName.."
read firstname lastname
echo "Hello $firstname ! Heard that your last name is $lastname."
The user input is broken to parts at white-space characters and the chunks are assigned to respective variables. Following are the possible scenarios with the number of words and number of variables.
| Scenario | Result |
|---|---|
| Number of Words = Number of Variables | Variables are assigned with words respectively |
| Number of Words < Number of Variables | Respective Words are assigned to Variables. Remaining variables remain empty. |
| Number of Words > Number of Variables | Respective Words are assigned to Variables. Except for the last variable is assigned the rest of input. |
Based on this, the first example where we read a line to variable could be considered as a third scenario in the table above with number of variables = 1.
Output
$ ./bash-readinput-example-2
Enter your firstname lastname..
Tutorial Kart
Hello Tutorial ! Heard that your last name is Kart.
$ ./bash-readinput-example-2
Enter your firstname lastname..
X Y
Hello X ! Heard that your last name is Y.
arjun@arjun-VPCEH26EN:~/workspace/bash/readinput$ ./bash-readinput-example-2
Enter your firstname lastname..
Monica
Hello Monica ! Heard that your last name is .
Read a full line safely with IFS= read -r
For user input that may contain spaces, backslashes, or leading whitespace, prefer IFS= read -r. The -r option prevents backslashes from being treated as escape characters, and IFS= helps preserve the line as typed.
#!/bin/bash
printf "Enter a file path: "
IFS= read -r filepath
echo "You entered: $filepath"
This form is useful when reading file paths such as C:\Users\Arjun\notes.txt, Linux paths with spaces, or any free-form text where the exact characters matter.
Read hidden password input with Bash read -s
Use read -s when the user should type input without displaying it on the terminal. This is commonly used for passwords, tokens, or short secrets. Add a separate echo after the command so the next output starts on a new line.
#!/bin/bash
read -s -p "Enter password: " password
echo
echo "Password length: ${#password}"
Avoid printing the password itself. The example prints only the number of characters to show that the value was read.
Read Bash input into an array with read -a
When a user enters a list of words separated by spaces, read -a can store the values in an indexed array.
#!/bin/bash
read -p "Enter three colors: " -a colors
echo "First color: ${colors[0]}"
echo "All colors: ${colors[*]}"
Array input is useful for simple lists, but it is still split using shell word-splitting rules. If items may contain spaces, read a full line first and parse it carefully.
Use a default value when Bash user input is empty
A script can accept a default value when the user presses Enter without typing anything. Use parameter expansion after reading the input.
#!/bin/bash
read -p "Enter environment [dev]: " environment
environment=${environment:-dev}
echo "Selected environment: $environment"
Here, ${environment:-dev} means: use the value of environment if it is not empty; otherwise use dev.
Validate Bash user input with a while loop
The read command stores what the user typed, but it does not validate the value by itself. Use a loop when your script must accept only a specific value.
#!/bin/bash
while true
do
read -p "Continue? (y/n): " answer
case "$answer" in
y|Y)
echo "Continuing..."
break
;;
n|N)
echo "Stopping..."
break
;;
*)
echo "Please enter y or n."
;;
esac
done
Always quote the variable in tests and case statements, as shown with "$answer". Quoting avoids errors when the input is empty or contains spaces.
Useful read options for Bash scripts
| Option | Use in Bash user input | Example |
|---|---|---|
-p | Display a prompt before reading input | read -p "Name: " name |
-r | Read backslashes as normal characters | IFS= read -r line |
-s | Hide typed input on the terminal | read -s password |
-a | Store split words in an array | read -a items |
-t | Stop waiting after a timeout in seconds | read -t 10 answer |
-n | Read a fixed number of characters | read -n 1 key |
Common mistakes when reading Bash user input
- Not quoting variables: use
"$name"instead of$namewhen testing or printing values that may contain spaces. - Using plain read for paths: use
IFS= read -r pathwhen backslashes or leading spaces should be preserved. - Printing secrets: if you use
read -sfor passwords, do not echo the password value back to the terminal. - Assuming input is valid: validate numbers, menu choices, and yes/no answers before using them in commands.
- Forgetting that read reads one line: for repeated input, place
readinside a loop.
Bash read user input checklist
- Use
read -pwhen the script needs a visible prompt. - Use
IFS= read -rfor full-line text, file paths, and values where backslashes matter. - Use
read -sfor passwords or tokens and avoid printing the secret value. - Use arrays only when space-separated input is acceptable.
- Validate user input before using it in file operations, commands, or conditional logic.
FAQs on Bash read user input
How do I read user input into a variable in Bash?
Use read variable_name. For example, read name reads one line from the terminal and stores it in the variable named name.
How do I show a prompt while reading Bash input?
Use the -p option: read -p "Enter name: " name. This displays the prompt and waits for the user to type a value.
How do I read a full line with spaces in Bash?
Use a single variable if you only need the full line. For safer full-line input, especially when backslashes matter, use IFS= read -r line.
How do I read password input silently in Bash?
Use read -s, usually with -p: read -s -p "Password: " password. Add echo after it to move the cursor to the next line.
Can Bash read input into multiple variables at once?
Yes. Use multiple variable names, such as read first last. Bash assigns words from the input to the variables from left to right; if there are extra words, the last variable receives the remaining text.
Key takeaways for reading user input in Bash
In this Bash Tutorial, we learned how to read user input, entered via standard input, to a variable and use the value in the script. For simple input, read variable is enough. For production-style shell scripts, use the appropriate read options, quote variables, preserve full-line text with IFS= read -r, and validate input before using it.
TutorialKart.com