The Linux read Command

The read command in Bash reads a line from the standard input and splits it into fields.

Linux read Command Examples

The read command in Linux is a built-in command used in shell scripts and command lines to read a line of input from the standard input (stdin) and assign it to a variable. Here are some examples of how to use the read command:

Basic Usage:

read varname
echo "You entered: $varname"

This script waits for the user to enter some text, which is then stored in the variable varname. When the user presses Enter, the script displays the entered text.

Reading Multiple Values:

echo "Enter two values:"
read var1 var2
echo "You entered: $var1 and $var2"

This allows the user to enter two separate values, which are stored in var1 and var2. The values should be separated by space.

Silent Input (Useful for Passwords):

read -s -p "Enter your password: " password
echo
echo "Password entered."

The -s option makes read silent, meaning it does not echo the input back to the terminal. This is useful for sensitive information like passwords. The -p option allows you to display a prompt.

Reading a Whole Line Including Spaces:

IFS= read -r line
echo "You entered: $line"

By setting IFS (Internal Field Separator) to an empty value and using -r (to prevent backslash escapes from being interpreted), the entire line of input, including spaces, is read into the variable line.

Setting a Timeout:

read -t 5 -p "Enter your name (you have 5 seconds): " name
echo "Hello, $name"

The -t option sets a timeout. In this example, read will wait for 5 seconds for the user to enter their name.

Reading from a File:

while IFS= read -r line
do
  echo "Line: $line"
done < filename.txt

This script reads lines from a file named filename.txt one by one.

Commandline Options of the read command

Here are some of its commonly used options:

  • -r: This option prevents backslashes from acting as escape characters. With -r, backslashes are treated as literal characters. This is generally recommended to ensure that the input is read accurately, without any unintended interpretation of backslash-escaped characters.

  • -p: This option allows you to specify a prompt that is displayed to the user. It's commonly used when you're interacting with the user and waiting for their input. For example, read -p "Enter your name: " name would display "Enter your name: " and then store the user's input in the variable name.

  • -a array: This option reads from the standard input and assigns the read words into an indexed array. This is useful for reading a line of input into an array, where each word becomes an element of the array.

  • -d delim: The -d option specifies a delimiter character to terminate the input line, rather than a newline. For example, read -d ":" var will read input until it encounters the colon character.

  • -n num: This option reads num characters rather than a complete line. It's useful when you need to restrict the input to a specific number of characters.

  • -s: Silent mode. When using -s, the input typed by the user is not displayed on the screen. This is typically used for sensitive input like passwords.

  • -t timeout: Specifies a timeout in seconds. If the user does not provide input within the specified number of seconds, the read command will terminate. This is useful for scripts where you don't want to wait indefinitely for user input.

  • -u fd: This option tells read to read input from the file descriptor fd instead of the standard input. This is an advanced feature used in scenarios where you are manipulating file descriptors in your script.

  • -N num: Similar to -n, but it reads num characters rather than bytes. This is particularly relevant in the context of multi-byte characters.

  • -E: Used with -p, this option disables the interpretation of backslash escapes and line-continuation in the prompt string.

  • -i text: This option provides an initial text for the read. It's particularly useful for providing a default value that the user can edit.

The read command is a powerful tool in Bash scripting for handling user inputs and parsing text. You can combine these options as needed to fit your specific requirements.

Share this page:

0 Comment(s)