Random Wisdom

Bash process substitution

by on Oct.03, 2008, under Linux, Software

From the Advanced Bash-Scripting Guide:

“Piping the stdout of a command into the stdin of another is a powerful technique. But, what if you need to pipe the stdout of multiple commands? This is where process substitution comes in.

Process substitution feeds the output of a process (or processes) into the stdin of another process.”

The syntax is:

 >(cmd_list)
 <(cmd_list)

Example: comparing the head of two files using diff

$ diff -u <(head -n3 /var/log/dmesg) <(head -n3 /tmp/dmesg)
--- /proc/self/fd/63 2009-05-26 19:52:45.144544140 +0100
+++ /proc/self/fd/62 2009-05-26 19:52:45.149544007 +0100
@@ -1,3 +1,3 @@
-Initializing cgroup subsys cpuset
-Initializing cgroup subsys cpu
-Linux version 2.6.27.21-170.2.56.fc10.i686 (mockbuild@xenbuilder2.fedora.redhat.com)
 (gcc version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC) )
 #1 SMP Mon Mar 23 23:37:54 EDT 2009
+Linux version 2.6.22.9-61.fc6 (brewbuilder@hs20-bc2-4.build.redhat.com)
 (gcc version 4.1.2 20070626 (Red Hat 4.1.2-13))
 #1 SMP Thu Sep 27 18:48:03 EDT 2007
+BIOS-provided physical RAM map:
+ BIOS-e820: 0000000000000000 - 000000000009f000 (usable)

The diff header clearly shows that file descriptors are used as the underlying mechanism.

:, ,

Leave a Reply