1

Question

In Bash, shell_variable=value bash child_command makes the shell_variable an environment variable in the child_command.

Is this a build-in shell (sh and bash) feature to promote a shell variable to an environment variable in a child process, and functions like env command?

Trying to find out a documentation explaining this but could not find yet. Appreciate if you could provide a reference.

hoge.sh

env | grep BUILD_ID

Example 1

BUILD_ID=hoge
bash hoge.sh

[result] (none displayed)

Example 2

BUILD_ID=hoge bash hoge.sh

[Result]
BUILD_ID=hoge

Example 3

env BUILD_ID=hogehoge bash hoge.sh 

[Result]
BUILD_ID=hogehoge

Reference

  • Documented here: gnu.org/software/bash/manual/… -- "... Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment." – glenn jackman Dec 5 '15 at 16:16
2

POSIX: Section 2.9.1 Simple Commands

... the variable assignments shall be exported for the execution environment of the command and shall not affect the current execution environment ...

Also in several shell manuals (search for "simple commands")


Examples

A variable shall not affect the current execution environment:

$ var=33; var=11 echo "$var"; echo "$var"
33
33

Variable assignments shall be exported to the execution environment:

$ var=33; var=11 sh -c 'echo "$var"'; echo "$var"
11
33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.