pipe - Difference between x | y and y <(x) in bash? -
is there difference between command1 | command2
, command2 <(command1)
?
for example, git diff | more
vs more <(git diff)
my understanding both take stdout of command2
, pipe stdin of command1
.
the main difference <(
...)
, called "process substitution", translated shell filename passed regular argument command; doesn't send command's standard input. means can't used directly commands such tr
don't take filename argument:
$ tr a-z a-z <(echo hello) usage: tr [-ccsu] string1 string2 tr [-ccu] -d string1 tr [-ccu] -s string1 tr [-ccu] -ds string1 string2
however, can put <
in front of <(
...)
turn input redirection instead:
$ tr a-z a-z < <(echo hello) hello
and because generates filename, can use process substitution commands take more 1 file argument:
$ diff -u <(echo $'foo\nbar\nbaz') <(echo $'foo\nbaz\nzoo') --- /dev/fd/63 2016-07-15 14:48:52.000000000 -0400 +++ /dev/fd/62 2016-07-15 14:48:52.000000000 -0400 @@ -1,3 +1,3 @@ foo -bar baz +zoo
the other significant difference pipe creates subshells can't have side effects in parent environment:
$ echo hello | read x $ echo $x # nothing - x not set
but process substitution, process inside parentheses in subshell; surrounding command can still have side effects:
$ read x < <(echo hello) $ echo $x hello
worth mentioning can write process >(
...)
, although there fewer cases that's useful:
$ echo hello > >(cat) hello
Comments
Post a Comment