thelinuxvault blog

Mastering the `read` Command in Linux: A Comprehensive Guide

In the vast landscape of Linux commands, the read command might not be as commonly known as some others like ls or cd. However, it plays a crucial role in certain scenarios, especially when dealing with terminal input and output handling. In this blog post, we'll explore what the read command is, how it works, and provide several examples to help you understand its usage better.

2026-06

Table of Contents#

  1. What is the read Command?
  2. Syntax of the read Command
  3. Example Usage of the read Command
    • Basic Input Acceptance
    • Accepting Input with a Prompt
    • Using read in Shell Scripts
  4. Common Practices and Best Practices
  5. References

What is the read Command?#

The read command in Linux is used to read a line of input from the standard input (usually the terminal). It can be used to prompt the user for information, which can then be used in further operations within a shell script or in an interactive terminal session.

Syntax of the read Command#

The basic syntax of the read command is as follows:

read [options] [variable]
  • options: There are several options available. For example, -p can be used to specify a prompt message.
  • variable: This is the name of the shell variable that will store the input read by the read command.

Example Usage of the read Command#

Basic Input Acceptance#

Let's start with a simple example. Suppose you want to read a user's name and store it in a variable. You can use the following command:

read name
echo "Your name is: $name"

In this example, when you run the first command (read name), the terminal will wait for you to enter some text. Once you press Enter, that text will be stored in the name variable. Then, the echo command will display the message along with the value of the name variable.

Accepting Input with a Prompt#

Using the -p option, you can provide a more user - friendly prompt. For instance:

read -p "Please enter your age: " age
echo "Your age is: $age"

Here, the user will see the prompt "Please enter your age: " before being able to enter their age. The entered age will be stored in the age variable and then displayed.

Using read in Shell Scripts#

Shell scripts often need user input. Consider the following simple script (input_script.sh):

#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
    echo "$num is greater than 10"
else
    echo "$num is less than or equal to 10"
fi

To make the script executable, use the command chmod +x input_script.sh. Then, when you run the script (./input_script.sh), it will prompt you for a number. Based on the number entered, it will display an appropriate message.

Common Practices and Best Practices#

  • Error Handling: When using read, it's a good practice to consider error handling. For example, if the user enters non - numeric data when a number is expected (as in the age or number examples above), you may want to add additional checks in your script.
  • Security: Be cautious when using read in scripts that may be run by multiple users. If the input is used in commands or operations that could have security implications (e.g., file operations), validate the input thoroughly.
  • Prompt Clarity: Always provide clear and concise prompts. This helps the user understand what kind of input is expected.

References#