Linux read Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the read command in Linux to capture user input and store it in a variable. You will also learn how to validate user input using the read command. The lab covers the purpose and syntax of the read command, how to read user input and store it in a variable, and how to validate user input.

The read command is a built-in command in Linux and is commonly used for interactive shell scripting. It allows you to capture user input and store it in a variable, which can then be used in your scripts or programs.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") subgraph Lab Skills linux/echo -.-> lab-422880{{"`Linux read Command with Practical Examples`"}} linux/read -.-> lab-422880{{"`Linux read Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the read Command

In this step, you will learn about the purpose and syntax of the read command in Linux. The read command is used to capture user input and store it in a variable.

The basic syntax of the read command is:

read [options] [variable_name]

Here, [options] are the optional flags you can use with the read command, and [variable_name] is the name of the variable where the user input will be stored.

Some common options for the read command include:

  • -p: Displays a prompt before waiting for user input.
  • -s: Suppresses the display of the user's input.
  • -n: Reads only the specified number of characters.
  • -t: Specifies a timeout in seconds for the read operation.

Example:

read -p "Enter your name: " name
echo "Hello, $name!"

Example output:

Enter your name: John
Hello, John!

In this example, the read command prompts the user to enter their name, and the input is stored in the name variable. The echo command then uses the value of the name variable to display a greeting.

Read User Input and Store it in a Variable

In this step, you will learn how to use the read command to capture user input and store it in a variable.

To read user input and store it in a variable, you can use the following syntax:

read variable_name

Here, variable_name is the name of the variable where the user input will be stored.

Example:

echo "What is your favorite color?"
read favorite_color
echo "Your favorite color is $favorite_color."

Example output:

What is your favorite color?
blue
Your favorite color is blue.

In this example, the user is prompted to enter their favorite color, and the input is stored in the favorite_color variable. The echo command then uses the value of the favorite_color variable to display a message.

You can also use the -p option with the read command to display a prompt for the user:

read -p "Enter your age: " age
echo "You are $age years old."

Example output:

Enter your age: 30
You are 30 years old.

In this example, the -p option is used to display the "Enter your age: " prompt before waiting for user input, and the input is stored in the age variable.

Validate User Input with the read Command

In this step, you will learn how to use the read command to validate user input.

One common way to validate user input is to use a while loop with the read command. This allows you to keep prompting the user for input until they provide a valid response.

Example:

while true; do
    read -p "Enter a number between 1 and 10: " num
    if [[ "$num" -ge 1 && "$num" -le 10 ]]; then
        echo "You entered: $num"
        break
    else
        echo "Invalid input. Please try again."
    fi
done

Example output:

Enter a number between 1 and 10: 15
Invalid input. Please try again.
Enter a number between 1 and 10: 7
You entered: 7

In this example, the read command is used inside a while loop to continuously prompt the user for input until they enter a number between 1 and 10. The if statement checks the user's input, and if it's valid, the loop is exited using the break command. If the input is invalid, an error message is displayed, and the loop continues.

You can also use the read command with the -n option to limit the number of characters the user can enter, or the -t option to set a timeout for the input.

Example:

read -n 1 -p "Enter 'y' or 'n': " answer
echo "You entered: $answer"

Example output:

Enter 'y' or 'n': y
You entered: y

In this example, the -n 1 option limits the user's input to a single character, and the -p option displays a prompt.

Summary

In this lab, you learned about the purpose and syntax of the read command in Linux, which is used to capture user input and store it in a variable. You also learned how to read user input and store it in a variable, as well as how to use the -p option to display a prompt for the user. Finally, you learned how to validate user input using the read command.

The read command has several optional flags, such as -p to display a prompt, -s to suppress the display of the user's input, -n to read only a specified number of characters, and -t to specify a timeout for the read operation. You can use these flags to customize the behavior of the read command to suit your needs.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like