Reading User Input Using read

Accepting dynamic input from users is essential for creating interactive shell scripts. The read command in Unix-like systems makes this possible, allowing scripts to prompt users, collect responses, and act on the provided data. In this article, we'll explore how to use read, common options, best practices, and real-world examples.


What is the read Command?

The read command is a built-in shell utility that lets scripts (and users at the command line) accept input. Its basic job is to read a line from standard input (such as the keyboard) and assign it to one or more shell variables.


Basic Usage

read variable_name

When the script encounters this line, it waits for the user to type something and press Enter. Whatever is typed is stored in variable_name.

Example: Simple Input

echo "What's your name?"
read name
echo "Hello, $name!"

Output:

What's your name?
Alice
Hello, Alice!

Prompting Inline

Instead of using echo and then read, you can provide a prompt directly with the -p flag (in Bash):

read -p "What's your favorite color? " color
echo "You chose: $color"

Reading Multiple Variables

You can read several values at once, separated by spaces:

echo "Enter your first and last name:"
read first last
echo "Hello, $first $last!"

Output:

Enter your first and last name:
Alice Johnson
Hello, Alice Johnson!

Silent Input (e.g., Passwords)

When asking for passwords or sensitive information, use the -s flag to keep input hidden:

read -sp "Enter your password: " password
echo
echo "Password received."

Setting a Timeout

If you want to wait only a certain number of seconds for input, use the -t option:

read -t 5 -p "Quick! Type something: " input
if [[ -z $input ]]; then
  echo "No input received in time."
else
  echo "You entered: $input"
fi

Handling Default Values

Sometimes you want to accept user input but provide a default if the user presses Enter without typing:

read -p "Enter your city [London]: " city
city=${city:-London}
echo "City is set to $city"

Reading from Files or Pipelines

The read command can also get input from a file or another command:

cat usernames.txt | while read username; do
  echo "User: $username"
done

Best Practices

  • Always quote variables. ("$name") This avoids issues with spaces or special characters.
  • Prompt clearly. Users should know what kind of input is expected.
  • Validate input. For critical scripts, check that input is not empty and matches the desired format.
  • Use arguments when appropriate. For non-interactive scripts, consider command line arguments ($1, $2, etc.) alongside read.

Common Pitfalls

  • Using read inside scripts with input redirection may behave differently.
  • In loops, read inside a pipeline runs in a subshell, and variable assignments may not persist outside the loop. Workarounds include redirects (while read ...; do ...; done < file) instead of pipelines.

Conclusion

The read command is fundamental for interactive shell scripting. By using its features wisely, you can make your scripts user-friendly, dynamic, and robust. Whether you’re collecting usernames, passwords, configuration options, or processing file input line by line, mastering read will significantly improve your shell scripting toolkit.


Summary:
Accept dynamic input in scripts using read.