Elegant way of diff'ing two variables?

13,276

echo. Clearly less weird.

#!/bin/bash

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

diff <(echo "$a") <(echo "$b")
Share:
13,276

Related videos on Youtube

Ole Tange
Author by

Ole Tange

I am strong believer in free software. I do not believe in Santa, ghosts, fairies, leprechauns, unicorns, goblins, and gods. Author of GNU Parallel.

Updated on September 18, 2022

Comments

  • Ole Tange
    Ole Tange over 1 year

    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?").

  • Andrew
    Andrew almost 6 years
    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
    Andrew almost 6 years
    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.
  • Loupax
    Loupax almost 5 years
    I am getting the same error even though I'm using #!/bin/bash, is there something I am missing?
  • Kusalananda
    Kusalananda almost 5 years
    @Loupax Are you maybe running the script with sh scriptname?
  • Loupax
    Loupax almost 5 years
    @Kusalananda You got me, I'll look up what shell sh actually is
  • Kusalananda
    Kusalananda almost 5 years
    @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.
  • Loupax
    Loupax almost 5 years
    @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