Introduction

Hello! In this article, we’ll learn about the read command.

read is a command for receiving input from users. Often used when creating interactive processes in shell scripts.

What is the read Command?

read is a command that stores keyboard input into variables.

For example, when you want to make scripts like “Please enter your name”, you use this. You can save what the user enters into a variable, so you can make interactive scripts.

Basic Syntax

1
read [options] variable_name

Main Options

Option Description
-p Display prompt message
-s Don’t display input on screen (for passwords)
-n number Read only specified number of characters
-t seconds Set timeout

Usage Examples

Example 1: Basic Usage

1
2
3
echo "Please enter your name:"
read name
echo "Hello, ${name}!"

Output:

1
2
3
Please enter your name:
Taro  ← User inputs
Hello, Taro!

Example 2: With Prompt

1
2
read -p "Please enter your name: " name
echo "Hello, ${name}!"

Output:

1
2
Please enter your name: Taro  ← User inputs
Hello, Taro!

Using -p lets you write it in one line without echo.

Example 3: Password Input (Hide Input)

1
2
3
read -sp "Please enter password: " password
echo
echo "Password has been set"

Output:

1
2
Please enter password:   ← Input not displayed
Password has been set

The -s option hides input on screen.

Example 4: Store in Multiple Variables

1
2
3
read -p "Enter name and age (space-separated): " name age
echo "Name: $name"
echo "Age: $age"

Output:

1
2
3
Enter name and age (space-separated): Taro 25
Name: Taro
Age: 25

Separated by spaces, stored in separate variables.

Example 5: Timeout Setting

1
2
3
4
5
if read -t 5 -p "Enter name within 5 seconds: " name; then
    echo "Hello, ${name}!"
else
    echo "Timed out"
fi

Times out if not input within 5 seconds.

Tips & Notes

  • Whitespace handling: By default, leading/trailing spaces are removed

    1
    2
    
    read -p "Input: " var
    # Even if you input "  test  ", $var becomes "test"
    
  • REPLY special variable: If you don’t specify a variable name, it automatically goes into REPLY

    1
    2
    
    read -p "Input: "
    echo "Input content: $REPLY"
    
  • Read from file: Can also read from files with redirection

    1
    2
    
    read line < file.txt
    echo $line  # First line of file is displayed
    
  • Yes/No confirmation: Common pattern

    1
    2
    3
    4
    5
    6
    
    read -p "Continue? (y/n): " answer
    if [ "$answer" = "y" ]; then
        echo "Proceeding"
    else
        echo "Canceling"
    fi
    

Practical Usage

Interactive Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash

read -p "Your name: " name
read -p "Favorite language: " lang
read -p "Age: " age

echo "=== Profile ==="
echo "Name: $name"
echo "Favorite language: $lang"
echo "Age: $age"

Scripts that collect user info, stuff like that.

Confirmation Prompt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash

read -p "Really delete? (yes/no): " confirm

if [ "$confirm" = "yes" ]; then
    echo "Executing deletion..."
    # rm -rf etc. dangerous operations
else
    echo "Canceled"
fi

Often used to confirm before dangerous operations.

Password Input

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

read -sp "Enter password: " pass1
echo
read -sp "Re-enter password: " pass2
echo

if [ "$pass1" = "$pass2" ]; then
    echo "Passwords match"
else
    echo "Passwords don't match"
fi

Can be used when setting passwords.

Use in Loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

while true; do
    read -p "Enter command (quit to exit): " cmd

    if [ "$cmd" = "quit" ]; then
        echo "Exiting"
        break
    fi

    echo "Command entered: $cmd"
done

Can make simple interactive shells too.

Summary

In this article, we learned about the read command.

Key Points:

  • Store user input in variables with read
  • -p for prompt display, -s to hide input
  • Can split into multiple variables with spaces
  • Essential when making interactive scripts

When you want interactive processing in shell scripts, this command is a must. Confirmation prompts, configuration input, etc. - lots of uses, so good to remember.

Keep learning Linux commands!