5

I have $a and $b. I want to run diff on those.

The best I have come up with is:

diff <(cat <<<"$a") <(cat <<<"$b")

But I have the district feeling that I am missing a clever Bash syntax to do that (as in "Why don't you just use foo?").

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Ole Tange
  • 33,591
  • 31
  • 102
  • 198

1 Answers1

18

echo. Clearly less weird.

#!/bin/bash

a="`seq 10`"
b="`seq 0 11`"

diff <(echo "$a") <(echo "$b")
Ole Tange
  • 33,591
  • 31
  • 102
  • 198
  • 1
    How do you use this in a bash script? Trying to output a diff of two strings using this syntax and I get "syntax error near unexpected token `('" – Andrew Jul 10 '18 at 20:03
  • 4
    I figured out why I couldn't get it to work. Process substitution is a bash feature, which is usually not available in `/bin/sh`. My bash script had the wrong shebang. Was `#!/bin/sh` but should have been `#!/bin/bash`. – Andrew Jul 10 '18 at 22:02
  • I am getting the same error even though I'm using `#!/bin/bash`, is there something I am missing? – Loupax May 21 '19 at 15:14
  • 2
    @Loupax Are you maybe running the script with `sh scriptname`? – Kusalananda May 21 '19 at 19:21
  • @Kusalananda You got me, I'll look up what shell `sh` actually is – Loupax May 22 '19 at 06:38
  • @Loupax It does not matter what it actually is, if you have written the script for `bash`, you should run your script as `./scriptname` (with a correct `#!`-line) or with `bash scriptname`. – Kusalananda May 22 '19 at 06:39
  • @Kusalananda Because of certain policies I can't call it directly but at least now I know what to look up. Thank you for the pointer! :D – Loupax May 22 '19 at 06:41