Bash – Process Substitution in Bash

bashprocess-substitutionshell-script

Can someone explain to me why I don't see the output of "date" from the below command? For N number for STDIN inputs, it's printing only for last (N-1) commands?

[root@RAJ-RHEL6 raj]# cat < <(date) <(hostname) <(uptime) <(cat /etc/resolv.conf)
RAJ-RHEL6.5
 02:22:59 up  2:36,  1 user,  load average: 0.00, 0.00, 0.00
nameserver 10.207.26.248
[root@RAJ-RHEL6 raj]#

Best Answer

You may only redirect the standard input stream from one place. You can't expect to be able to redirect it from several files or process substitutions in a single command.

The command

cat < <(date) <(hostname) <(uptime) <(cat /etc/resolv.conf)

is the same as

cat <(hostname) <(uptime) <(cat /etc/resolv.conf) < <(date)

i.e., you are giving cat three input files, and then you redirect the output of date into its standard input.

The cat utility will not use its standard input stream if it's given files to work with, but you can get it to do so by using the special - filename:

cat - <(hostname) <(uptime) <(cat /etc/resolv.conf) < <(date)

Note also that the last process substitution is useless and the command is better written written as

cat - <(hostname) <(uptime) /etc/resolv.conf < <(date)

or, without that redirection of the output of date, as

cat <(date) <(hostname) <(uptime) /etc/resolv.conf

or, with a single command substitution,

cat <( date; hostname; uptime; cat /etc/resolv.conf )

or, without process substitutions,

date; hostname; uptime; cat /etc/resolv.conf

Related: