Javatpoint Logo
Javatpoint Logo

Linux read Command

The Linux read command is used to read the contents of a line into a variable. This is a built-in command for Linux systems. Therefore, we do not need to install any additional tools. It is an easy tool to take user input when creating a bash script. It is a powerful utility and as important as echo command and positional parameter. It is used to split the words that are tied to the shell variable. Primarily, it is used to take user input but can be used to implement functions while taking input.

Syntax:

The basic syntax of the read command is as follows:

How to use read command?

The read command can be used with and without arguments. Let's look at the various usage of the read command:

  • Default Behaviour

If we pass the read command without any argument, it will take a line as user input and store it in a built-in variable 'REPLY'. Execute the command as:

The above command will ask for the user input. Type the user input and press ENTER key to save it. To display the entered content, execute the command as:

The above command will display the stored input from the 'REPLY' variable.

Consider the below output:

Linux read Command
  • Specify the variables to store the values

We can specify the variables to store the input. If the number of specified variables is lower than the entered words, it will store all the remaining words in the last variable by default. Consider the below command:

The above command will store the 'Javatpoint' in 'var1', 'is' in 'var2', and all remaining words in 'var3'. To verify the values, execute the below command:

The above command will display the variable values. Consider the below output:

Linux read Command

By default, the read command considers the <space>, <tab>, and <newline> characters as words and splits the input according to these characters. But we can pass the input using multiple line, separating each line by a backslash '\' character. Consider the below command:

The above command will take each line as an input character and will store it into the specified variable. To verify the values, execute the below command:

Consider the below output:

Linux read Command
  • The Internal Field Separator

The internal field separator (IFS) is used to separate the output fields and determines the word boundaries in a given line. We can set it according to our needs. Execute the below script:

From the above script, the output will be separated by a semicolon. Consider the below output:

Linux read Command

Options

Following are some useful argument options that are used with the read command:

  • read -p

The '-p' option is used for the prompt text. It reads the data along with some hint text. This hint text helps us while entering the text such as what to enter. Consider the below command:

The above command will ask for the name, enter the name. The name will store to the 'REPLY' variable. To display the variable value, execute the below command:

Consider the below output:

Linux read Command
  • read -n

The '-n' option limits the length of the character in the entered text. It will not let you enter text more than the specified number of characters. After reaching the limit of characters, it automatically stops reading. To limit the character up to six, execute the below command:

The above command will not let us enter more than 6 character. Consider the below output:

Linux read Command
  • read -s

The '-s' option is used for security purpose. It is used to read the sensitive data. By using this option, the entered text won't appear in the terminal. We can use other options with this option. Characters are read in this option. Primarily, it is used to read the passwords from the keyboard. Consider the below command:

The above command will prompt for the password, when we will type password, it will not be displayed on the terminal. Consider the below output:

Linux read Command

From the above output, the password has not shown on the terminal, but the value has stored in the variable.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA