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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -