How to Read User Input in Bash

How to Read User Input in Bash

How to Read User Input in Bash

A script that only ever does one fixed thing is useful, but a script that can respond to what I type while it’s running is far more powerful. That’s what user input handling gives you. In this article, I’ll show you every practical way I read input in Bash — from simple prompts to silent password entry to reading entire files interactively.

The read Command

The read command is the backbone of user input in Bash. It reads a line from standard input and stores it in one or more variables.

Basic Syntax

read variable_name

Example 1: Reading a Single Value

#!/bin/bash

echo "What is your name?"
read name
echo "Hello, $name! Nice to meet you."

Sample run:

What is your name?
Sarah
Hello, Sarah! Nice to meet you.

Example 2: Prompting on the Same Line with -p

Rather than using a separate echo before read, you can combine them:

#!/bin/bash

read -p "Enter your favorite color: " color
echo "You chose $color"

Sample run:

Enter your favorite color: blue
You chose blue

Example 3: Reading Multiple Values at Once

#!/bin/bash

read -p "Enter your first and last name: " first last
echo "First name: $first"
echo "Last name: $last"

Sample run:

Enter your first and last name: John Smith
First name: John
Last name: Smith

Bash splits the input by whitespace and assigns each word to the corresponding variable. If there are more words than variables, the last variable gets everything remaining.

Reading Passwords Silently

When reading sensitive input, you don’t want it echoed to the screen. The -s flag hides the typed characters.

#!/bin/bash

read -sp "Enter your password: " password
echo
echo "Password received (hidden for security)"

Sample run:

Enter your password: 
Password received (hidden for security)

Nothing appears on screen as you type, which is exactly the behavior you want for sensitive data.

Setting a Timeout for Input

Sometimes you want a script to move on if the user doesn’t respond in time. The -t flag lets you set a timeout in seconds.

#!/bin/bash

if read -t 5 -p "Do you want to continue? (yes/no): " answer
then
    echo "You said: $answer"
else
    echo
    echo "No response received within 5 seconds. Exiting."
fi

If the user doesn’t type anything within 5 seconds, read fails and the else branch runs.

Limiting Input Length

The -n flag reads a fixed number of characters and then automatically continues, without waiting for Enter.

#!/bin/bash

read -n 1 -p "Press any key to continue..." key
echo
echo "You pressed: $key"

This is handy for “press any key” prompts in interactive scripts.

Reading Input into an Array

The -a flag splits input into an array, one element per word.

#!/bin/bash

read -p "Enter three numbers separated by spaces: " -a nums
echo "First number: ${nums[0]}"
echo "Second number: ${nums[1]}"
echo "Third number: ${nums[2]}"

Sample run:

Enter three numbers separated by spaces: 10 20 30
First number: 10
Second number: 20
Third number: 30

Reading Input from a File

read isn’t just for keyboard input — it can also read lines from a file when combined with redirection.

#!/bin/bash

while read -r line
do
    echo "Line content: $line"
done < "data.txt"

Each iteration of the loop pulls one line from data.txt into the line variable.

Reading with a Custom Delimiter

By default, read splits input using the IFS (Internal Field Separator), which is whitespace. You can change this for CSV-like data.

#!/bin/bash

IFS=',' read -p "Enter name,age,city: " name age city
echo "Name: $name"
echo "Age: $age"
echo "City: $city"

Sample run:

Enter name,age,city: Alice,30,Chicago
Name: Alice
Age: 30
City: Chicago

Reading Command-Line Arguments

Although not technically the read command, arguments passed when running the script are a form of user input too:

#!/bin/bash

echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $@"
echo "Number of arguments: $#"

Sample run:

$ ./script.sh apple banana
Script name: ./script.sh
First argument: apple
Second argument: banana
All arguments: apple banana
Number of arguments: 2

How This Works Internally

Real-World Use Cases

read -p "Enter the domain name for your site: " domain
echo "server_name $domain;" >> nginx.conf
read -p "This will delete all files in /tmp/cache. Continue? (y/n): " confirm
if [[ $confirm == "y" ]]
then
    rm -rf /tmp/cache/*
    echo "Cache cleared."
else
    echo "Operation cancelled."
fi
read -p "Username: " user
read -sp "Password: " pass
echo
while IFS=',' read -r name score
do
    echo "$name scored $score points"
done < scores.csv

Best Practices

Security Considerations

Optimization Tips

Troubleshooting Common Issues

Frequently Asked Questions

Q: How do I read input without waiting for the Enter key? A: Use read -n 1 to read a single character immediately without requiring Enter.

Q: How do I hide password input in Bash? A: Use the -s flag: read -sp "Password: " password.

Q: Can I set a default value if the user just presses Enter? A: Yes — after reading, check if the variable is empty and assign a default: name=${name:-"Guest"}.

Q: What’s the difference between $* and $@? A: Both represent all positional parameters, but "$@" preserves each argument as a separate word (recommended), while "$*" joins them into a single string.

Common Mistakes to Avoid

Summary

Reading user input is what turns a static script into an interactive tool. The read builtin, combined with flags like -p, -s, -t, -n, and -a, covers nearly every input scenario you’ll encounter — from simple prompts to hidden password entry to parsing structured data. Combine that with positional parameters ($1, $2, $@) for command-line arguments, and you have everything you need to build genuinely interactive Bash scripts.

References

Exit mobile version