There are two ways to set the pass variables from your current shell a running program,
Either use the export
built-in with syntax as
$ export MYVALUE=5
$ echo "MYVALUE is $MYVALUE"
MYVALUE is 5
This syntax allows the variable to take effect in current shell in all the subsequent sub-shells you are invoking( for command-substitution or process-substitution, etc) and the variable stays alive even after the sub-shells are terminated.
(or) as asked in the question, if you directly send it to the command as
$ MYVALUE=5 bash -c 'echo "MYVALUE is $MYVALUE"'
MYVALUE is 5
the value is passed only to the sub-shell(the one started with bash -c
) and has no effect on parent shell once it exits. You can observe the MYVALUE
from the above syntax now, it will be empty.
$ echo $MYVALUE
$
Hope this answers your question.