Chapter 5. Redirection, Pipe and Shell Script

Read (Read and Store Input)

Read (Read and Store Input)
Tag:

The read command is used to read the contents of a line (from a command-line input or a file) into a variable. The initial setting of the variable is $REPLY. If the variable is not specified, $REPLY is used as a default variable.

read command typical flow

Unlike other commands, the read command is used in a more interactive way.
There are three steps to use the read command.

1. Declare a variable

By running the read command, you can declare a variable. If you don't specify it, $REPLY becomes the variable. After the declaration, the command waits for the input of the variable.

For example, use ABC as a variable. After the enter key is hit, the command line waits for command line input.

Command Line - INPUT
read ABC

2. Make an input to the variable (assign value)

After the variable is set, you can make an input to the variable. For example, you can type 123 to assign the value to the variable.

Command Line - INTERACTIVE
123

3. Return an assigned value

You can use the variable in another command such as the echo command to return the assigned value to the variable.

If you run the echo command with $ABC, the command line returns the assigned value 123.

Command Line - INPUT
echo $ABC
Command Line - RESPONSE
123

read command with a file input

You can use a file for the read command input.

For example, we created the error.txt file in the redirection section. We will use the file to demonstrate how the command works. In the example below, you can see that the read command reads the error.txt file and the echo command returns the content.

Command Line - INPUT
read ABC < error.txt
echo $ABC
Command Line - RESPONSE
ls: cannot open directory '/lost+found': Permission denied

From this example, you can also see that variable ABC is overwritten by the new input.

IdeaNote: Multi-line text file

The error.txt file contains only one-line data; however, there are files with multiple lines. If you use the read command with a multi-line text file as an input, only the first line of the text file is assigned as a variable.

The read command is used to read the contents of a line (from a command-line input or a file) into a variable. The initial setting of the variable is $REPLY. If the variable is not specified, $REPLY is used as a default variable.

read command typical flow

Unlike other commands, the read command is used in a more interactive way.
There are three steps to use the read command.

1. Declare a variable

By running the read command, you can declare a variable. If you don't specify it, $REPLY becomes the variable. After the declaration, the command waits for the input of the variable.

For example, use ABC as a variable. After the enter key is hit, the command line waits for command line input.

Command Line - INPUT
read ABC

2. Make an input to the variable (assign value)

After the variable is set, you can make an input to the variable. For example, you can type 123 to assign the value to the variable.

Command Line - INTERACTIVE
123

3. Return an assigned value

You can use the variable in another command such as the echo command to return the assigned value to the variable.

If you run the echo command with $ABC, the command line returns the assigned value 123.

Command Line - INPUT
echo $ABC
Command Line - RESPONSE
123

read command with a file input

You can use a file for the read command input.

For example, we created the error.txt file in the redirection section. We will use the file to demonstrate how the command works. In the example below, you can see that the read command reads the error.txt file and the echo command returns the content.

Command Line - INPUT
read ABC < error.txt
echo $ABC
Command Line - RESPONSE
ls: cannot open directory '/lost+found': Permission denied

From this example, you can also see that variable ABC is overwritten by the new input.

IdeaNote: Multi-line text file

The error.txt file contains only one-line data; however, there are files with multiple lines. If you use the read command with a multi-line text file as an input, only the first line of the text file is assigned as a variable.

Tag: