Question on bash redirection

Hi,

Can I get some explanation around this bash redirection?
From what I have read, x < y means call the shell to redirect the output of y into x.
Does this mean that this sequence of commands is executed from right to left?

diff <(sort testfile.txt) <(sort testfile2.txt)

Thanks,

This is a so-called "process substitution", where a process (or, rather, its outout) is put into the place of a file. First, there is a diff command:

diff file1 file2

which would list the differences between these two files. Now, both "file1" and "file2" are substituted by a process:

<(sort testfile.txt)

So, the file "testfile.txt" is sorted and the result of this sorting (the output of this process) is used as input to the diff command instead of a regular file. Likewise with the second file. This mechanism is called "process substitution".

I hope this helps.

bakunin

1 Like

x < y means: run command x and feed its input (stdin) from file y.
It is true that file y is opened for reading before command x runs.
diff <(sort testfile.txt) <(sort testfile2.txt) means: open a device ("/dev/fd/..."), run sort and redirect its output to the device, open another device, run another sort and redirect its output, run diff with the two devices as arguments (that diff treats as input filenames).

1 Like