How to use the read command in Bash?

前端 未结 8 1762
执念已碎
执念已碎 2020-12-07 23:54

When I try to use the read command in Bash like this:

echo hello | read str
echo $str

Nothing echoed, while I think str<

相关标签:
8条回答
  • 2020-12-08 00:57

    Other bash alternatives that do not involve a subshell:

    read str <<END             # here-doc
    hello
    END
    
    read str <<< "hello"       # here-string
    
    read str < <(echo hello)   # process substitution
    
    0 讨论(0)
  • 2020-12-08 00:58

    Typical usage might look like:

    i=0
    echo -e "hello1\nhello2\nhello3" | while read str ; do
        echo "$((++i)): $str"
    done
    

    and output

    1: hello1
    2: hello2
    3: hello3
    
    0 讨论(0)
提交回复
热议问题