How to Use read Command in Linux
Intermittently while sharing by means of your Linux product you could have to rile individuals for input or read input from documents, or even yearn to kit timeouts. You can lug out these vacancies and also innumerable others by means of the read
command and also its assorted patchworks.
This short article will unquestionably educate you the ethics of read
command and also its patchworks using innumerable code examples.
- What Is the Read Command
- Read Command Replacements
- How to Read Input Making gain application of of Read Command
- How to Incite Input Making gain application of of Read Command
- Constricting User Input While Analysis
- Dividing Result Fields Making gain application of of IFS
- Position Timeout on Read
- How to Hide User Input
What Is the Read Command
In Linux, you can gain application of the read
command to ensnare debtor input or read a pitch upwards from perfunctory input (stdin). This command reviews the entirety digit of bytes from the imparted record descriptor and also storefronts them in the barrier. After that, it comebacks the digit of bytes read, zero, or an misstep.
For example, if the digit or count is zero, it refers to the expire of the record. Yet on wins, it comebacks the digit of bytes read. If the read command locates some missteps, it comebacks -1.
Before seeing the read command patchworks, permit’s first attractiveness at the phrase texture of the read command:
read [options] [name…]
Proper here, the options
perfunctory enumerates assorted flags that are overseen to modify the behaviour of the read command. In addition, the name
perfunctory enumerates the moniker of innumerable variables that are overseen to store front the input. If zero tags are issued, the input is preserved in the $REPLY
ceremony variable.
Read Command Replacements
The Bash read command possesses innumerable patchworks to manipulate debtor input. Some patchworks wear’t need peripheral criteria, while others lug out.
Make it feasible for’s dissect some patchworks we can gain application of by means of the read command:
Replacements | Descriptions |
---|---|
-a | It storefronts the input as an hodgepodge instead of separate variables. |
-s | Runs steadly, foreseeing input is not displayed on the incurable |
-e | enables readline array commercialize, permitting reading the input pitch upwards |
-i
| provides an initial input value that appears at the prompt when using readline |
-p | displays the specified prompt before reading the input |
-u | reads from a specified file descriptor rather than the standard input (stdin) |
-d | allows you to specify an input line delimiter instead of the default newline character |
-t | sets a timeout period for input; if the input is not received within this time, read returns a failure |
-r | when set, backslashes are not treated as escape characters |
-n | reads only the specified number of characters |
Type the following command to output the read command help menu:
head --help
How to Read Input Making gain application of of Read Command
The the majority of mere way to gain application of the read command is to gain application of it without any type of arguments or patchworks. As quickly as you implement the read command one by one, it will unquestionably ask you to bargain the input that you yearn to read. After presenting input, it will unquestionably exit and also store front it in its default variable named REPLY
.
Make it feasible for’s apprehend this as an example:
read <user_input>

Now, after presenting input, permit’s brandish it using the resemble command:
echo $REPLY

While reading the input privilege, you can in addition store front it in any type of other fussy variables. For example, to store front the expire upshot applicable into a variable, form the read
command heeded by the variable moniker:
read variable1 <user_input>

Now, to brandish the expire upshot, you have to gain application of the echo
command by means of the variable that storefronts your privilege:
echo $variable1

Analysis A digit of Worths
There is zero balanced way to read innumerable perks using the read
command. Yet, you can suspension the singular input sentence applicable into innumerable words and also store front them in unlike variables.
Make it feasible for’s contemplate the consequent example:
read variable1 variable2 variable3 <user_input>

Proper here, you store front the first word of the sentence in the first variable, and also the second word in the second variable, and also with one voice the lingering words in the last issued variable.
Make it feasible for’s rejoinder the upshot using the consequent command:
echo <$variabl_namee>

Analysis From a Document
While read is largely for debtor input, you can in addition gain application of it to read queues from a record. To lug out this, just gain application of the while
triviality, echo
command, and also read
command heeded by a fussy variable moniker:
while read line; do echo "$line" done < samplefile.txt

Proper here, the while
triviality reviews each pitch upwards of the “samplefile.txt” and also records it in the variable line
. After reading with one voice the queues of a record, the echo
command verifies the privilege of the pitch upwards.
Analysis Input in a Loop
You can in addition ensnare debtor input in a reoccured sequence by using a read by means of a while triviality. This is helpful as quickly as you yearn to amass innumerable inputs or recur until a fussy ailment is fulfilled.
For example, permit’s read innumerable inputs and also in addition brandish them on the incurable:
while read line; do echo "Line: $line" done

In addition, the triviality reiterates until it fetches an expire-of-record (EOF) signal, generally by emboldening Ctrl + D.
How to Incite Input Making gain application of of Read Command
You can in addition variation interactive prompts that will unquestionably brandish before the debtor input. You can achieve this by using the -p
replacement along by means of the read command.
Make it feasible for’s brandish a custom rile and also in addition ensnare the input:
read -p "Enter your Name: " name

Constricting User Input While Analysis
Want to manipulate what individuals can get in? Usage the -n
replacement by means of read to recognize your preferred individuality dimension. For example, if you yearn to ensnare the first 4 personalities of the debtor input, gain application of the listed below command:
read -n 9 variable

After implementing the overhead command, it permits you to get in singular 9 personalities. Constricting debtor input while reading will unquestionably apprehend out the need for a delimiter and also in addition doesn’t suspension the input applicable into words.
Dividing Result Fields Making gain application of of IFS
You can gain application of the Inner Demographic Separator (IFS) variable by means of the read command to ascertain how to suspension the input applicable into separate industries. By default, IFS applications whitespace personalities (tab, newline, and also enclosure) as demographic separators. Yet, you can modify the IFS to match your requires.
For example, you can recognize the colon (:) privilege as a delimiter:
IFS=':' read name password <user_input> echo "Name: $name, Password: $password"

Through the IFS variable, you can ensnare innumerable pitch upwards inputs from the debtor and also separate them based on the separator privilege.
Position Timeout on Read
You can in addition configure a timeout for the read command using the -t
replacement. This is helpful if you yearn to gain sure the script doesn’t rely on debtor input for a long time.
Make it feasible for’s kit a 10-second timeout using the -t
replacement:
read -t 10 -p "Enter your name (10 seconds timeout): " name <user_input> echo "Hello, $name!"

If the debtor doesn’t form anything within 10 seconds, the script reiterates implementation.
How to Hide User Input
Intermittently, you could yearn to hide the debtor’s input, such as as quickly as entering breakable infos favor passwords. You can attain this using the -s
replacement of the read command:
read -s -p "Enter your password: " password <user_input>

After storing the password in the defined variable, you can brandish it in the incurable using the echo
command:
echo -e "nPassword: $password"

Proper here, the -e
flag permits the interpretation of vacation cheat, and also n
vacation sequence includes a brand name-conspicuous pitch upwards before the upshot.
Layer Upward
By the time you finishing reading this short article and also kneading by means of the examples, you’ll be familiar by means of using the read command in the Linux incurable. Yet, there are innumerable peripheral commands to dissect. Bolster reading to find out the simplistic commands for beginners.
Image credit history: Gabriel Heinzer by means of Unsplash. Unanimously screenshots by Haroon Javed.