Find the answer to your Linux question:
Results 1 to 3 of 3
Hi Guys, I'm writing a bash script and need to assign the result of diff to a variable. diff -u file1 file2 I've tried stuff like var=${diff -u $file1 $file2} ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    1

    How to assign diff result to a variable in bash script

    Hi Guys,

    I'm writing a bash script and need to assign the result of diff to a variable.

    diff -u file1 file2

    I've tried stuff like

    var=${diff -u $file1 $file2} where file1 and file2 are variable holding filenames and get an error "bad substitution". I've also tried putting the $file1 and $file2 in quotes along with several other variations. Same error.

    What is the correct syntax?

    Thanks...

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,964
    Code:
    var=$(diff -u $file1 $file2)
    You must always face the curtain with a bow.

  3. #3
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by Irithori View Post
    Code:
    var=$(diff -u $file1 $file2)

    That will fail if either filename contains whitespace or other pathological characters. Quote the variables:

    Code:
    var=$(diff -u "$file1" "$file2")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •