read Command in Linux

Topics Covered

Overview

The read command in Linux is an in-built command-line utility that is primarily used for accepting user input or reading a line of input from a standard input (stdin). Its functionality doesn't stop there though, with a range of flags to add to its utility, from controlling the input delimiter to setting a timeout for the input. It is widely used in shell scripts for taking user input during the execution of the script.

Syntax of read Command in Linux

The basic syntax of the read command in Linux is as follows:

Where:

  • options: These are flags that alter the behaviour of the read command. Some of the most commonly used options include '-p' for displaying a prompt, '-t' for setting a timeout, and '-a' for reading into an array.
  • name: The variables where the input from the read command will be stored. If more than one variable name is given, the line of input is split into words and each word is assigned to a variable.

Options in read Command in Linux

  1. -p: Displays a prompt to the user before reading the input.

    For example -

    Output:

    This command prompts the user to enter their name and stores the input in the 'name' variable.

  2. -t: Sets a timeout for the read command. If no input is provided within the specified number of seconds, the command ends.

    For example -

    Output:

    This command waits for 5 seconds for the user to enter their name. If no input is provided within 5 seconds, the command ends.

  3. -a: Reads the input into an array instead of a single variable.

    For example -

    This command reads the input into an array named 'names'.

Example Usages

  • Basic usage of the read command:

    Explanation: This command waits for the user to input a line of text, which is then stored in the 'name' variable.

  • Reading multiple inputs into separate variables:

    Explanation: The command waits for the user to input two words, which are then stored in the 'firstName' and 'lastName' variables respectively.

Tips

  • The read command in Linux is not limited to taking user input, it can also be used to read file content line by line in shell scripting.

  • The IFS (Internal Field Separator) variable is crucial when reading multiple inputs, as it determines how the input is split into separate variables.

Advanced Use Cases of read Command in Linux

  • Reading input with a prompt and a timeout:

    Output:

    Explanation: The command prompts the user to enter their name within 5 seconds. If no input is provided, the command ends.

  • Reading input into an array:

    Explanation: The command reads the user input into an array named 'colors'. Each word of input becomes an element of the array.

  • Reading from a file using the read command in a loop:

    Explanation: This command reads the content of 'file.txt' line by line and prints each line. The '-r' option prevents backslashes from being interpreted as escape characters.

Conclusion

  • The read command in Linux is a powerful tool for handling user input and reading file content.

  • Its utility can be expanded with options, allowing it to set a timeout for input, display a prompt, and read input into an array among other things.

  • The read command's ability to integrate with other commands makes it a cornerstone in shell scripting.