"here string"和"here string"之间有什么区别?和回声+管道 [英] What's the difference between "here string" and echo + pipe
问题描述
想知道here-string(here-document)和管道的正确用法是什么.
Wondering what is the right use of here-string (here-document) and pipe.
例如
a='a,b,c,d'
echo $a | IFS=',' read -ra x
IFS=',' read -ra x <<< $a
这两种方法都可以.那么这两个功能之间有什么区别?
Both methods work. Then what would be the difference between the two functionality?
关于阅读"的另一个问题是:
Another problem that I have about "read" is that:
IFS=',' read x1 x2 x3 x4 <<< $a
不起作用,x1的值为"a b c d",x2,x3,x4的值为
does not work, x1 is valued as "a b c d", and x2, x3, x4 has no value
但是如果:
IFS=',' read x1 x2 x3 x4 <<< "$a"
我可以得到x1 = a,x2 = b,x3 = c,x4 = d一切都好!
I can get x1=a, x2=b, x3=c, x4=d Everything is okay!
有人可以解释吗?
预先感谢
推荐答案
在管道中,创建了两个新进程:一个用于执行echo
命令的shell,另一个用于执行read
的shell.命令.由于两个子外壳程序都在完成后退出,因此x
变量在管道完成后不可用. (在bash
4中,引入了lastpipe
选项,以允许管道中的最后一条命令在当前外壳程序(而不是子外壳程序)中执行,从而减轻了此类管道的问题.)
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).
在第二个示例中,here字符串(此处是由单行组成的一种特殊类型的here文档)不需要任何额外的处理,因此x
的值实际上是在当前shell中设置的,因此可供稍后在脚本/会话中使用.
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.
这篇关于"here string"和"here string"之间有什么区别?和回声+管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!