1

If I do:

diff <(cat old) <(cat new)

Hoe does that work internally for the diff program? And how I do the same trick with a bash script?

1

You can try it yourself:

echo <(echo) <(echo)

Diff just reads from both the files.

If you want to use <(...) as a parameter to your bash script, just keep in mind you can't "rewind" the file (or reopen). So once you read it, it's gone. You can use read to process it line by line, you can grep it or whatever. If you need to process it more than once, either save its content to a variable

input=$(cat "$1"; printf x) # The "x" keeps the trailing empty lines.
input=${input%x}

or copy it to a temporary file and read it over and over:

tmp=$(mktemp)
cat "$1" > "$tmp"
1

For the diff program there is no difference as if you type diff old new. The <(cmd) operator redirects the stdout of cmd1 to a fifo and then pass the fifo as an argument to diff.

See an example:

user@host:~$ echo <(ls)
/dev/fd/63

/dev/fd/63 is the fifo containing the output of ls, see cat <(ls) prints the output of the fifo, hence the ouput of ls.

So, the diff program is executed as something like this:

diff /dev/fd/63 /dev/fd/64

And both fifos contain the output of the cmds. For diff they are files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.