How to use read command in Linux

 You can perform these and many other tasks using the read command and its various options.

This article will guide you through the basics of the read command and its options through some examples.

What is the Read command?

In Linux, you can use the read command to capture user input or read a line from standard input (stdin). This command reads the total number of bytes from the given file descriptor and stores them in the buffer. The command then returns the number of bytes read, zero, or error.

For example, if the number or count is zero, the command will refer to the end of the file. But when successful, the command returns the number of bytes read. If the read command finds some errors, it returns -1.

Before exploring the read command options, let's first look at the syntax of the read command:

read [options] [name…]

Here, the options parameter specifies various flags used to modify the behavior of the read command. Additionally, the name parameter specifies the names of multiple variables used to store input data. If no name is provided, input will be retained in the $REPLY bash variable.

read command options

Bash's read command has many options for controlling user input. Some options do not need additional parameters, while others do.

Let's explore some of the options we can use with the read command:

Option Describe
-a It stores input data as an array instead of separate variables.
-S Runs silently, meaning input data is not displayed on the terminal
-e Enables readline library support and reading input lines
-i Provides the initial input value that appears at the prompt when using readline
-p Displays the specified prompt before reading the input
-u Read from a specified file descriptor instead of standard input (stdin)
-d Allows specifying an input line separator instead of the default newline character
-t Set input timeout; If input is not received within this time, the read command will return an error
-r When set, backslashes are not treated as escape characters
-n Read only the specified number of characters

Enter the following command to output the read command help menu:

head --help

How to read input using read command

The simplest way to use the read command is to use it without any arguments or options. When executing just the read command, the command will ask you for the input you want to read. After providing input, the command exits and stores that input in a default variable named REPLY.

Let's take the following example:

read 

How to use read command in Linux Picture 1

Now, after providing input, display it using echo command:

echo $REPLY

How to use read command in Linux Picture 2

While reading the input value, you can also store it in any other specific variable. For example, to store the result in a variable, enter the read command followed by the variable name:

read variable1 

How to use read command in Linux Picture 3

Now, to display the results, you need to use the echo command with the variable storing the value:

echo $variable1

How to use read command in Linux Picture 4

Read many values

There is no direct way to read multiple values ​​using the read command. However, you can split an input sentence into multiple words and store them in different variables.

Consider the following example:

read variable1 variable2 variable3 

How to use read command in Linux Picture 5

Here, you store the first word of the sentence in the first variable, the second word in the second variable, and all the remaining words in the last variable provided.

Let's return the output with the following command:

echo <$variabl_namee>

How to use read command in Linux Picture 6

Read from a file

Although the read command is primarily for user input, you can also use it to read lines from a file. To do this, simply use a while loop, an echo command, and a read command followed by the specific variable name:

while read line; do echo "$line" done < samplefile.txt

How to use read command in Linux Picture 7

Here, the while loop reads each line of "samplefile.txt" and records it in the line variable. After reading all lines of the file, the echo command will display the value of that line.

Read input in loop

You can also capture user input in a repeating sequence using the read command with a while loop. This is useful when you want to collect multiple inputs or continue until a specific condition is met.

For example, let's read multiple inputs and also display them on the terminal:

while read line; do echo "Line: $line" done

How to use read command in Linux Picture 8

Furthermore, the loop continues until the end of file (EOF) signal is received, usually by pressing Ctrl + D .

3.5 ★ | 2 Vote

May be interested

  • 11 uses of ps command in Linux11 uses of ps command in Linux
    for system administrators, ps is a frequently used tool. the ps command is used to list the processes currently running on the system, with many available filtering and display modes via flags and arguments.
  • How to Delete Read-Only Files on LinuxHow to Delete Read-Only Files on Linux
    there are several ways to help you delete files read-only on linux. if it's a file you own, you can change the file permissions or use the sudo command to delete the original file. however, if you cannot delete files with write permissions (or see a 'read only file system' error message), you need to change the drive with the appropriate permissions. this article will show you how to delete read-only files on any linux distribution, including ubuntu and linux mint.
  • Instructions for using zforce command on LinuxInstructions for using zforce command on Linux
    the gzip command is a popular tool used to compress / decompress files in linux. tipsmake.com presents the basics of this tool in the article: some basic terminal commands in linux mint 11.
  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Distribute file access with chmod commandDistribute file access with chmod command
    unix and linux operating systems decentralize access to files and directories using three access parameters, read (read), write (write) and execute (run) to delegate permissions to three groups of objects, including: system owners, administrative groups and users.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.