7

Please, observe:

$ diff <(echo a) <<<b
diff: missing operand after '/dev/fd/63'
diff: Try 'diff --help' for more information.

I know <(...) works fine:

$ diff <(echo a) <(echo b)
1c1
< a
---
> b

I also know <<< works fine in general:

$ cat <<<a
a

So what is the right way to call it with diff?

AdminBee
  • 21,637
  • 21
  • 47
  • 71
mark
  • 387
  • 1
  • 9

3 Answers3

30

The diff command expects filename arguments, while the here string <<< writes to standard input. However man diff tells us

If a FILE is '-', read standard input.

So

$ diff <(echo a) - <<<b
1c1
< a
---
> b

The same would apply for a here document:

$ diff <(echo a) - << \EOF
b
EOF
1c1
< a
---
> b
steeldriver
  • 78,509
  • 12
  • 109
  • 152
11

On systems that have /dev/fd/x devices, you can do:

diff /dev/fd/3 3<< EOF3 /dev/fd/4 4<< EOF4
some text
EOF3
some other text
EOF4

using the Bourne shell's << here-document redirection operator.

diff /dev/fd/3 3<<< 'some text' /dev/fd/4 4<<< 'some other text'

Using zsh's <<< here-string redirection operator.

In zsh, you can also do:

diff <(<<<'some text') <(<<<'some other text')
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
2

You can combine <() with cat <<<:

diff <(echo a) <(cat <<<b)

Steeldriver's answer works well if you just need to use <<< for one argument, but if you need it for both this will work:

diff <(cat <<<a) <(cat <<<b)

But it's not clear why you would need a here-string for this. There's little difference between <(cat <<<a) and <(echo a)

Barmar
  • 9,648
  • 1
  • 19
  • 28