1

I'm trying to create a separate bash process and echo a variable which is set inside of it with no success. Nothing gets echoed.

bash -c "COMMIT_DIFF_FILE=diffs.diff && echo -e ${COMMIT_DIFF_FILE}"

What could be the problem here? Many thanks in advance!

Mihai Galos
  • 1,707
  • 1
  • 19
  • 38
  • Did you try `export $VARIABLE`? – trinaldi Dec 12 '18 at 08:07
  • as far as I understand, export can be used to inject a variable into the environment. Here, I'm creating a separate process which sets a new variable and then echos it. – Mihai Galos Dec 12 '18 at 08:08
  • 1
    `export` makes the variable available to sub-processes. BTW, I got there's a [thread](https://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export) with more info! – trinaldi Dec 12 '18 at 08:09

1 Answers1

1

You have to quote it right.

bash -c 'COMMIT_DIFF_FILE="diffs.diff" && echo "$COMMIT_DIFF_FILE"'

diffs.diff

You are quoting command to bash -c in double quotes which gets expanded in current shell where variable is not present.

If you want to use double quotes then escape $:

bash -c "COMMIT_DIFF_FILE=diffs.diff && echo \${COMMIT_DIFF_FILE}"
anubhava
  • 761,203
  • 64
  • 569
  • 643