read Command in Linux



read command is a built-in utility in Linux used to read a line of text from standard input (such as the keyboard or a file) and assign it to one or more variables. It plays a crucial role in shell scripting, enabling interactive scripts that can accept user input, process files, and execute commands based on the input data. This functionality is essential for creating dynamic and responsive scripts that can handle a variety of tasks, from simple user prompts to complex data processing.

Table of Contents

Here is a comprehensive guide to the options available with the read command −

Syntax of read Command

The general syntax for the read command is −

read [options] variable_name

Where −

  • options − Various flags that modify the behavior of the command.
  • variable_name − The name of the variable(s) to which the input will be assigned.

read Command Options

Below are the options you can apply with the read command on Linux −

Option Description
-a array Assigns the input words to sequential indices of the array variable, starting at zero.
-d delim Continues reading until the first character of DELIM is encountered, rather than stopping at a newline.
-e Uses the Readline library to obtain the input line.
-i text Sets TEXT as the initial text for Readline to display.
-n chars Returns after reading NCHARS characters, honoring delimiters if fewer characters are read first.
-N chars Returns only after reading exactly NCHARS characters, unless EOF is encountered or read times out.
-p prompt Displays the string PROMPT before reading input, without a trailing newline.
-r Disables the interpretation of backslashes as escape characters.
-s Suppresses echoing of input characters when reading from a terminal (useful for passwords).
-t time Times out and returns failure if a complete line is not read within TIMEOUT seconds.
-u fd Reads input from the file descriptor FD instead of standard input.

Examples of read Command in Linux

Let's explore a few practical examples of using the read command with the modified options −

  • Reading User Input with a Prompt
  • Capturing Password Input in Silent Mode
  • Setting a Timeout for User Input
  • Assigning Input to an Array
  • Utilizing a Custom Delimiter
  • Setting Initial Text with Readline

Reading User Input with a Prompt

You can prompt the user and capture their input using the -p option −

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

This command displays a prompt asking for the user's name and then greets them with their input.

read Command in Linux2

Capturing Password Input in Silent Mode

For reading a password without displaying it, use the -s option −

read -s -p "Enter your password: " password
echo "Password received."
read Command in Linux3

This command reads the password in silent mode and confirms receipt.

read Command in Linux4

Setting a Timeout for User Input

To handle input with a timeout, utilize the -t option −

read -t 10 -p "Enter something within 10 seconds: " input
echo "You entered: $input"

This command waits for 10 seconds for the user to input data and then displays the entered text.

read Command in Linux5

Assigning Input to an Array

To read values into an array, you can use the -a option −

read -a array -p "Enter multiple values separated by spaces: "
echo "First value: ${array[0]}"
echo "Second value: ${array[1]}"
read Command in Linux6

This command reads multiple values separated by spaces into an array and displays the first two values.

read Command in Linux7

Utilizing a Custom Delimiter

To read input with a specific delimiter, make use of the -d option −

read -d ":" -p "Enter text ending with a colon: " input
echo "You entered: $input"
read Command in Linux8

This command reads input until a colon (:) is encountered and then displays the entered text.

read Command in Linux9

Setting Initial Text with Readline

For setting initial text with Readline, make use of the -i option −

read -e -i "default" -p "Enter your input: " input
echo "You entered: $input"
read Command in Linux10

This command sets "default" as the initial text displayed by Readline.

read Command in Linux11

Conclusion

The read command is a built-in utility in Linux that plays a crucial role in shell scripting. It enables interactive scripts that accept user input, process files, and execute commands, making it essential for creating dynamic and responsive scripts.

By understanding the various options and practical applications, users can effectively utilize the read command to enhance their scripting capabilities. Whether you're handling simple user prompts or complex data processing tasks, mastering the read command opens up a wide range of possibilities in the Linux environment.

Advertisements