Bash is a powerful, sh-compatible command language interpreter commonly used in Unix-based systems to execute commands and automate tasks through shell scripts. It allows you to process commands directly in a terminal or read and execute instructions stored in a file.
One essential feature of Bash is its ability to accept user input dynamically. This capability makes it possible to create interactive scripts where users can provide data through the command line or during runtime.
In this article, we’ll dive into how to retrieve user input in Bash, including reading input directly from the command line, storing it in variables, and practical examples to demonstrate its usage. Whether you're new to Bash scripting or looking to refine your skills, this guide will help you master handling user input effectively.
Read User Input
If we want to request input from the user, we will use the read command. This command accepts input and saves it to a variable. It reads a single line from the Bash shell.
Now let’s understand the basic keywords of bash commands so that we can learn Shell Scripting - Read User Input:
Commands | Description |
Echo | It displays the argument on standard output. |
pwd(print working directory) | It displays the current working directory. |
ls(list) | It lists the content of the directory. |
cd(change directory) | It is used to change the directory. |
mkdir(make directory) | It is used to make a directory. |
rmdir(remove directory) | It is used to remove the directory. |
touch | It is used to create blank files. |
rm(remove) | It removes the files from a directory. |
mv(move) | It is used to move the file from one directory to another. |
p | It is used to prevent shell reading from user-controlled files. |
$ | It is used to print the variable values. |
sp | It is used to keep the user information hidden. |
You can run the bash command in an online code editor like replit.
Syntax
read<variablename>
Example 1
# example to show Shell Scripting - Read User Input
# Asking the user for their name
echo Hello Ninja what is your name
read varname
echo It\'s nice to meet you $varname
Output

Explanation
In line 2 of the example, you can see that we are printing a statement. In the next line, we ask the user to write their name.
Finally, we print the user name with an echo command in the third line. You can see that we have used a ‘\’ in the statement. This is because we want extra space between the letters.
What will happen if you don't pass any variable with a read command?
If you don't pass any variable with a read command, then we can pass the built-in variable called REPLY for displaying the input. Let’s understand it with an example.
Let’s consider the above example. We need to use the REPLY function for writing without a variable, which gets the user input without any variable as it reads the last line as whole input from the user.
#Example to show Shell Scripting - Read User Input
# Asking the user for their name
echo Hello Ninja coder, my name is ashish and what is yours?
read
echo It\'s nice to meet you "$REPLY"
Output

Example 2
Let’s understand how we can get Shell Scripting - Read User Input using p and sp command. We will use sp command because it hides the user information. You can see it in the below output.
Syntax
p<Variablename>
#Example to show Shell Scripting - Read User Input
#!/bin/bash
# Ask the user for login details
read -p 'Username: ' user
read -sp 'Password: ' pass
echo
echo Thankyou $user we now have your login details now
Output

Multiple inputs using an array
To take input as an array, we will use ‘-a’ command which helps us to take multiple inputs from the user.
#Example to show Shell Scripting - Read User Input
#!/bin/bash
# Reading multiple inputs using an array
echo "Enter the names : "
read -a name
echo "Entered names are : ${name[0]}, ${name[1]}."
Output

Now the question arises that we have seen two methods:
🔺Command line arguments.
🔺Read inputs during script execution
Which one is better in Shell Scripting - Read User Input?
Command-line arguments should be the first priority as they are the most convenient for users. This is because the data is saved in their command history, allowing them to return to data quickly to the user.
But in this case, if you want to take the inputs like passwords, then reading inputs during script execution will be the better option as it hides the data from being displayed.
Summary of Common read Command Options
- -p: Displays a prompt before input.
- -s: Hides user input (useful for passwords).
- -a: Stores input into an array.
- -t: Sets a timeout for input.
- -n: Limits the number of characters read.
Frequently Asked Questions
1. How do I retrieve user input in a Bash script?
In Bash, user input can be retrieved using the read command. This command pauses the execution of the script and waits for the user to provide input. Once the input is entered, it is stored in a variable for further use. This is commonly employed in interactive scripts to make them more dynamic and user-focused.
2. How can I store user input into a variable in Bash?
The read command in Bash allows you to directly store user input into a variable. By specifying a variable name with the read command, any input provided by the user is automatically assigned to that variable. This stored value can then be accessed and manipulated within the script.
3. Can Bash scripts take input directly from the command line?
Yes, Bash scripts can accept input directly from the command line in the form of arguments. These arguments are passed to the script when it is executed and can be accessed using positional parameters, such as $1 for the first argument, $2 for the second, and so on. This method is particularly useful for automating tasks where input values are known beforehand.
4. How can I prompt the user for input with a message in Bash?
To prompt the user for input, you can combine a message with the read command. Displaying a message before taking input helps guide the user on what type of data they should provide. This approach enhances the usability of the script, ensuring the interaction is clear and intuitive.
5. Can I validate or restrict user input in Bash?
Bash provides mechanisms to validate or restrict user input, such as conditional statements, loops, and pattern matching. Input validation ensures that the data provided by the user meets specific criteria, such as being a number, a specific format, or within a certain range. This process is critical for creating robust and error-free scripts.