`read` is a shell utility used for reading strings from the standard input stream, or from a file descriptor.
Questions tagged [read]
355 questions
67
votes
3 answers
What is the meaning of read -r?
In order to understand another answer (by glenn jackman):
find / -type d -print0 | while read -r -d '' dir; do ls -ltr "$dir" | sed '$!d'; done
the first step is to understand the usage of the option -r of the read command.
First, I thought, it…
Abdul Al Hazred
- 25,760
- 23
- 64
- 88
51
votes
2 answers
Iterating over multiple-line string stored in variable
I read that it is bad to write things like for line in $(command), the correct way seem to be instead:
command | while IFS= read -r line; do echo $line; done
This works great. But what if what I want to iterate on is the contents of a variable, not…
Jonathan H
- 2,303
- 3
- 20
- 27
43
votes
1 answer
How to get total read and total write IOPS in Linux?
How do I get read and write IOPS separately in Linux, using command line or in a programmatic way? I have installed sysstat package.
Please tell me how do I calculate these separately using sysstat package commands.
Or, is it possible to calculate…
shas
- 2,578
- 4
- 17
- 31
33
votes
2 answers
In bash, read after a pipe is not setting values
With ksh I'm using read as a convenient way to separate values:
$ echo 1 2 3 4 5 | read a b dump
$ echo $b $a
2 1
$
But it fails in Bash:
$ echo 1 2 3 4 5 | read a b dump
$ echo $b $a
$
I didn't find a reason in the man page why it fails, any…
Emmanuel
- 4,167
- 2
- 23
- 30
21
votes
4 answers
Why some shells `read` builtin fail to read the whole line from file in `/proc`?
In some Bourne-like shells, the read builtin can not read the whole line from file in /proc (the command below should be run in zsh, replace $=shell with $shell with other shells):
$ for shell in bash dash ksh mksh yash zsh schily-sh heirloom-sh…
cuonglm
- 150,973
- 38
- 327
- 406
18
votes
4 answers
Bash: interactive remote prompt
I have a script which connects to a remote server and check if some package is installed:
ssh root@server 'bash -s' < myscript.sh
myscript.sh:
OUT=`rpm -qa | grep ntpdate`
if [ "$OUT" != "" ] ; then
echo "ntpdate already installed"
else
yum…
Anthony Ananich
- 7,234
- 5
- 31
- 45
17
votes
1 answer
What is the meaning of "<&3" and "done < file11 3< file22"
I have the following script:
while read lineA && read lineB <&3
do
echo "$lineA"
echo "$lineB"
echo
done < file11 3< file22
This code is working fine. But I don't understand these:
<&3
done < file11 3< file22
Gaurav
- 171
- 3
13
votes
3 answers
Use read as a prompt inside a while loop driven by read?
I have a use case where I need to read in multiple variables at the start of each iteration and read in an input from the user into the loop.
Possible paths to solution which I do not know how to explore --
For assignment use another filehandle…
Debanjan Basu
- 333
- 1
- 2
- 6
13
votes
3 answers
read command in zsh throws error
In zsh, running the command read -p 'erasing all directories (y/n) ?' ans, throws the error,
read: -p: no coprocess
But in bash, it prints a prompt. How do I do this in zsh?
user93868
13
votes
3 answers
Use bash's read builtin without a while loop
I'm used to bash's builtin read function in while loops, e.g.:
echo "0 1
1 1
1 2
2 3" |\
while read A B; do
echo $A + $B | bc;
done
I've been working on some make project, and it became prudent to split files and store…
Bananguin
- 7,796
- 2
- 25
- 57
13
votes
3 answers
How to handle backspace while reading?
How do I handle the backspaces entered, it shows ^? if tried & how read counts the characters, as in 12^?3 already 5 characters were complete(though all of them were not actual input), but after 12^?3^? it returned the prompt, weird. Please…
Keyshov Borate
- 1,301
- 5
- 16
- 34
12
votes
3 answers
bash: Some issue when using read <<<"$VARIABLE" on a read-only root partition. Any known workarounds?
Just by coincidence I had to use my ATA-ID-to-device-name script (found here: https://serverfault.com/questions/244944/linux-ata-errors-translating-to-a-device-name/426561#426561) on a read-only / partition. In case you're curious, it was an Ubuntu…
syntaxerror
- 2,236
- 2
- 27
- 49
11
votes
3 answers
For what purpose does "read" exit 1 when EOF is encountered?
The bash man page says the following about the read builtin:
The exit status is zero, unless end-of-file is encountered
This recently bit me because I had the -e option set and was using the following code:
read -rd '' json <
jesse_b
- 35,934
- 12
- 91
- 140
11
votes
2 answers
A while loop and an here-document - what happens when?
I have this while loop and here-document combo which I run in Bash 4.3.48(1) and I don't understand its logic at all.
while read file; do source ~/unwe/"$file"
done <<-EOF
x.sh
y.sh
EOF
My question is comprised of these parts:
What does…
Arcticooling
- 1
- 12
- 44
- 103
11
votes
4 answers
Multiple commands during an SSH inside an SSH session
I have a local machine which is supposed to make an SSH session to a remote master machine and then another inner SSH session from the master to each of some remote slaves, and then execute 2 commands i.e. to delete a specific directory and recreate…
mgus
- 259
- 1
- 4
- 9