What's the difference between “here string” and echo + pipe

后端 未结 1 860
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 21:09

Wondering what is the right use of here-string (here-document) and pipe.

For example,

a=\'a,b,c,d\'
echo $a | IFS=\',\' read -ra x
IFS=\',\' read -r         


        
相关标签:
1条回答
  • 2020-12-09 21:37

    In the pipeline, two new processes are created: one for a shell to execute the echo command, and one for a shell to execute the read command. Since both subshells exit after they complete, the x variable is not available after the pipeline completes. (In bash 4, the lastpipe option was introduced to allow the last command in a pipeline to execute in the current shell, not a subshell, alleviating the problem with such pipelines).

    In the second example, no extra process is need for the here string (a special type of here document that consists of a single line), so the value of x is in fact set in the current shell, making it available for use later in the script/session.

    0 讨论(0)
提交回复
热议问题