Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting two files to standard input

Tags:

redirect

bash

sh

There are several unix commands that are designed to operate on two files. Commonly such commands allow the contents for one of the "files" to be read from standard input by using a single dash in place of the file name.

I just came across a technique that seems to allow both files to be read from standard input:

comm -12 <(sort file1) <(sort file2)

My initial disbelieving reaction was, "That shouldn't work. Standard input will just have the concatenation of both files. The command won't be able to tell the files apart or even realize that it has been given the contents of two files."

Of course, this construction does work. I've tested it with both comm and diff using bash 3.2.51 on cygwin 1.7.7. I'm curious how and why it works:

  • Why does this work?
  • Is this a Bash extension, or is this straight Bourne shell functionality?
  • This works on my system, but will this technique work on other platforms? (In other words, will scripts written using this technique be portable?)
like image 912
Bobby Eickhoff Avatar asked Jan 19 '11 16:01

Bobby Eickhoff


People also ask

What is the use of 2 >& 1?

The 1 denotes standard output (stdout). The 2 denotes standard error (stderr). So 2>&1 says to send standard error to where ever standard output is being redirected as well. Which since it's being sent to /dev/null is akin to ignoring any output at all.

What is standard input redirection?

Redirection is a feature in Linux such that when executing a command, you can change the standard input/output devices. The basic workflow of any Linux command is that it takes an input and give an output. The standard input (stdin) device is the keyboard. The standard output (stdout) device is the screen.

Can you redirect stdin?

There are always three default files [1] open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen). These, and any other open files, can be redirected.

What is meant by redirecting input output?

On a command line, redirection is the process of using the input/output of a file or command to use it as an input for another file. It is similar but different from pipes, as it allows reading/writing from files instead of only commands. Redirection can be done by using the operators > and >> .


2 Answers

Bash, Korn shell (ksh93, anyway) and Z shell all support process substitution. These appear as files to the utility. Try this:

$ bash -c 'echo <(echo)'
/dev/fd/63
$ ksh -c 'echo <(echo)'
/dev/fd/4
$ zsh -c 'echo <(echo)'
/proc/self/fd/12

You'll see file descriptors similar to the ones shown.

like image 141
Dennis Williamson Avatar answered Sep 23 '22 02:09

Dennis Williamson


This is a standard Bash extension. <(sort file1) opens a pipe with the output of the sort file1 command, gives the pipe a temporary file name, and passes that temporary file name on the comm command line.

You can see how it works by getting echo to tell you what's being passed to the program:

echo <(sort file1) <(sort file2)
like image 34
Tim Robinson Avatar answered Sep 19 '22 02:09

Tim Robinson