bash - What's the difference between "here string" and echo + pipe -
wondering right use of here-string (here-document) , pipe.
for example,
a='a,b,c,d' echo $a | ifs=',' read -ra x ifs=',' read -ra x <<< $a
both methods work. difference between 2 functionality?
another problem have "read" that:
ifs=',' read x1 x2 x3 x4 <<< $a
does not work, x1 valued "a b c d", , x2, x3, x4 has no value
but if:
ifs=',' read x1 x2 x3 x4 <<< "$a"
i can x1=a, x2=b, x3=c, x4=d okay!
can explain this?
thanks in advance
in pipeline, 2 new processes created: 1 shell execute echo
command, , 1 shell execute read
command. since both subshells exit after complete, x
variable not available after pipeline completes. (in bash
4, lastpipe
option introduced allow last command in pipeline execute in current shell, not subshell, alleviating problem such pipelines).
in second example, no process need here string (a special type of here document consists of single line), value of x
in fact set in current shell, making available use later in script/session.
Comments
Post a Comment