user746461
user746461

Reputation:

Save git output to a variable

I have content= $(git diff --cached $line) in my bash script.

enter image description here

But when I execute it, bash throws error

gqqnbig MINGW64 /c/Website/Lender (master)
$ ./hook.sh
Admin/Xpress/BusinessAccountTypeRole_Edit.aspx
diff: unknown option -- git
diff: Try 'diff --help' for more information.

Why doesn't $(git diff --cached $line) work, and how to fix it?

Upvotes: 2

Views: 6979

Answers (1)

David Z
David Z

Reputation: 131600

It doesn't work because you put a space after the equals sign.

content= $(git diff --cached $line)
        ^
      there

That space means Bash sets the environment variable content to the empty string for the command specified by the rest of the line, instead of setting the shell variable content to the result of running the command.

To fix it, remove the space.

Upvotes: 15

Related Questions