I am trying to understand why the following happens:
After setting up a variable like var1=test
in the current shell, without exporting it (so it will not be available in child processes) and execute /bin/bash -c "echo $var1"
right after, I can see the unexported variable.
I am aware of the fact that a builtin command like "echo" does not fork() or exec(), so it would have access to the unexported $var1
or about the fact that a SUBSHELL would have access to the unexported variables too.
But in my case, /bin/bash -c "command"
is not a SUBSHELL either as /bin/bash -c "echo $BASH_SUBSHELL"
would show me "0
". And I thought it should behave the same as an external command, so -> fork
-> exec
and inherit environment variables from the parent.
So, for example, I see the following behaivor:
[nimus@localhost ~]$ echo $var1
test
[nimus@localhost ~]$ /bin/bash
[nimus@localhost ~]$ echo $var1
[nimus@localhost ~]$ exit
exit
[nimus@localhost ~]$ /bin/bash -c "echo $var1; echo $BASH_SUBSHELL"
test
0
[nimus@localhost ~]$
So how is /bin/bash -c "command"
different then an external command or fork
and exec
a new whole bash Shell like above?
Thanks!
echo
(a builtin or not) does not "have access to the unexported$var1
". It doesn't get$var
, it gets the expanded string(s). – Kamil Maciorowski Jan 26 '21 at 16:45