builtin - How to use the read command in Bash? -
when try use read
command in bash this:
echo hello | read str echo $str
nothing echoed, while think str
should contain string hello
. can please me understand behavior?
the read
in script command fine. however, execute in pipeline, means in subshell, therefore, variables reads not visible in parent shell. can either
move rest of script in subshell, too:
echo hello | { read str echo $str }
or use command substitution value of variable out of subshell
str=$(echo hello) echo $str
or more complicated example (grabbing 2nd element of ls)
str=$(ls | { read a; read a; echo $a; }) echo $str
Comments
Post a Comment