Diff: NamedPipesInBash

Differences between version 2 and previous revision of NamedPipesInBash.

Other diffs: Previous Major Revision, Previous Author

Newer page: version 2 Last edited on January 14, 2007 6:37 pm by PhilHollenback Revert
Older page: version 1 Last edited on June 13, 2005 10:59 am by PhilHollenback Revert
@@ -1,7 +1,11 @@
-Here's something that somebody just recently told me. This is a useful and completely non-obvious tip, so I'm documenting it here in hopes that others will find it useful. 
+Here's something that somebody just recently told me. This is a  
+ useful and completely non-obvious tip, so I'm documenting it here in  
+ hopes that others will find it useful. 
  
-My specific problem was that I wanted to diff the output of two processes. You can't do this with normal shell reidrection, the best you can do is: 
+My specific problem was that I wanted to diff the output of two  
+ processes. You can't do this with normal shell reidrection, the best  
+ you can do is: 
  
 * send the output of one process to a tempfile 
 * diff the tempfile and the output of the second process, i.e.: 
  
@@ -11,11 +15,22 @@
 </verbatim> 
  
 that works, but it requires a temporary file. 
  
-The answer to this problem is process substitution. Bash has some process substitution operators (<(foo) and >(foo)) which are very poorly explained in the bash man page (and trust me, I've read that man page __a lot__). foo can be any command that produces output on stdout. Bash execs the command, creates a named pipe from the output, and replaces the operator with the name of that pipe. You can then read stdout from that pipe as you would Thus in this case the output of foo might be fed through the file /dev/fd/64. Now our diff example can be written like this: 
+The answer to this problem is process substitution. Bash has some  
+ process substitution operators (<(foo) and >(foo)) which are very  
+ poorly explained in the bash man page (and trust me, I've read that  
+ man page __a lot__). = foo= can be any command that produces output on  
+ stdout. Bash execs the command, creates a named pipe from the output,  
+ and replaces the operator with the name of that pipe. You can then  
+ read stdout from that pipe as you would from a regular file. Thus in  
+ this case the output of foo might be fed through the file /dev/fd/64.  
+ Now our diff example can be written like this: 
  
 <verbatim> 
 # diff <(/sbin/lsmod) <(ssh sys1 /sbin/lsmod) 
 </verbatim> 
  
-Note you can stuff stdout __into__ named pipes with the >(foo) operator too, thus the output of a process can be sent into another process which you invoke on the command line. This is more flexible than the standard pipe mechanism. 
+Note you can stuff stdout __into__ named pipes with the >(foo)  
+ operator too, thus the output of a process can be sent into another  
+ process which you invoke on the command line. This is more flexible  
+ than the standard pipe mechanism. 

version 2

Here's something that somebody just recently told me. This is a useful and completely non-obvious tip, so I'm documenting it here in hopes that others will find it useful.

My specific problem was that I wanted to diff the output of two processes. You can't do this with normal shell reidrection, the best you can do is:

  • send the output of one process to a tempfile
  • diff the tempfile and the output of the second process, i.e.:
# /sbin/lsmod >/tmp/lsmod.tmp
# ssh sys1 /sbin/lsmod | diff /tmp/lsmod.tmp -

that works, but it requires a temporary file.

The answer to this problem is process substitution. Bash has some process substitution operators (<(foo) and >(foo)) which are very poorly explained in the bash man page (and trust me, I've read that man page a lot). foo can be any command that produces output on stdout. Bash execs the command, creates a named pipe from the output, and replaces the operator with the name of that pipe. You can then read stdout from that pipe as you would from a regular file. Thus in this case the output of foo might be fed through the file /dev/fd/64. Now our diff example can be written like this:

# diff <(/sbin/lsmod) <(ssh sys1 /sbin/lsmod)

Note you can stuff stdout into named pipes with the >(foo) operator too, thus the output of a process can be sent into another process which you invoke on the command line. This is more flexible than the standard pipe mechanism.



Our Founder
ToolboxClick to hide/show
RecentChanges Click to hide/show