Introduction to Linux Read Command
In the Linux ecosystem, the read command is used to read from the file descriptor. In other words, the read command is mostly used in the bash environment. In bash, the read command is used for the word segmentation of the string under the Linux environment. The read command is a built-in utility available in the Linux ecosystem.
read [options] [name...]
- read: We can use the read keyword in the syntax or command. It will accept the two arguments as input i.e. the options and name. As per the requirement, the read command will do the word segmentation of the string.
- options: We can provide the different flags as options that are compatible with the read command.
- name: The read command, the name parameter will specify to store the actual words from the split operation.
How Does Linux Read Command Work?
The read utility is a very powerful utility in the Linux bash environment. As discussed earlier, it is used for the work segmentation of the string. The read command is working with the option and the name. It will accept the different flags as an option and the name parameters will store the actual words from the split operation.
Basically the read is a built command in the Linux environment. It has come with the basic operating system packages. Hence there is no need to add any additional packages in the Linux environment.
|-l||It will display all the names of Readline functions in the environment.|
|-p||It will print the readline function names and the bindings that they can be re-read.|
|-P||It will display the current readline function names and bindings in the environment.|
|-s||It will print the readline key sequences bound to macros as well as the strings output. The outputs will then re-read.|
|-S||It will print the readline key sequences bound to macros as well as the strings output.|
|-v||It will print the readline variable names and the values. The output can be re-read.|
|-V||It will print all the current readline variable names and values available in the environment.|
Examples to Implement Linux Read Command
Below are the examples of Linux Read:
#1. Read Command
In read command, we are able to read in the user inputs. The same input we can pass to different commands or jobs. So, in the next instance, the different commands or the jobs can perform the execution operation with the read command as in input.
Explanation: The read command can accept user input. Just we need to right the read command and enter the input message or variable value (refer screenshot 1 (a)). By default, the message will store in the system variable. When we will call the system variable “REPLY”. The entered message will display (refer screenshot 1 (b)).
#2. Read Command with variable
In the Linux environment, we are having the functionality to access the read command value in different variables. As per the requirement, we can define the variable in the read command.
Explanation: As per the above read command, we are forcefully using the own variable to store the input value (refer screenshot 2 (a)). The same variable will use to see the read command message (refer screenshot 2 (b)).
#3. Read Command in Shell Mode
We can use the “cd” command to come in the Read Command form any location. Similarly, we can achieve the same functionality via the “tilde (~)” option.
echo "Which topic is it?";read a;echo "Welcome to $a"
Explanation: In live shell mode, we can also use the read command. It will ask the user input. Once the user will enter the input value. It will print the message as per the read command output.
#4. Read Command with “-p” Option
In read command, we are having the functionality to provide the hint while entering the user inputs in the shell prompt. For providing any hint on the shell, we need to use the “-p” option with the read command.
read -p "Please Enter Your Mobile No : "
Explanation: As per the above command, we are giving hit to the end-user for entering the relevant character. It will help to give an important message while entering any user input.
#5. Read Command with “-n” Option
In read command, we are having the functionality to restrict the specific number of characters. For restricting the number of characters, we need to use the “-n” option with the read command.
read -n 6 -p "Enter Pin Code : "
Explanation: In the above command, we are restricting the user input with a specific threshold (we have used the 6 characters). If the character will excide more than 6 then the prompt will automatically vanish.
#6. Read Command with “-s” Option
In read command, we can secure sensitive data. While entering critical information, we can hide the information. To secure the critical information, we need to use the “-s” option with the read command.
read -s -p "Enter Password : "
Explanation: As per the above read command, we are accepting the user input in a secure way. The user input will not display on screen but the read command is accepting user input.
We have seen the uncut concept of “Linux Read Command” with the proper example, explanation and command with different outputs. The read command is widely used in the shell and application-level jobs. We can use the read command with different ways like accepting user inputs, secure input, character restriction, input hint, etc.
This is a guide to Linux Read. Here we discuss an overview of the Linux Read Command and its different commands along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –