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.
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