0

I'm using ZSH. Why does the last command not output anything?

$ cat create_foo.sh 
foo=bar

$ source create_foo.sh && echo $foo
bar

$ unset foo

$ bash -c "source create_foo.sh && echo $foo"

From the last command I don't get any output.

ub_marco
  • 140
  • 1
  • 5

1 Answers1

4

Because $foo is getting substituted with an empty string in the double quotes.

You need to either use single quotes or escape the $ sign:

$ bash -c 'source create_foo.sh && echo $foo'
bar

$ bash -c "source create_foo.sh && echo \$foo"
bar
sachav
  • 1,316
  • 8
  • 11
  • Thanks a lot, that's it. Here's an answer that explains the difference between single quotes and double quotes: https://stackoverflow.com/a/6697781/2285820 – ub_marco Aug 10 '18 at 11:35