Skip to main content
link to the POSIX shell specifications
Source Link
xhienne
  • 18.3k
  • 2
  • 58
  • 71

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.

Here are the relevant parts of the bash manual:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

(...)

Simple Command Expansion

If no command name results [from command expansion], the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment.

Note: bear in mind that this is not specific to bash, but specified by POSIX.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.

Here are the relevant parts of the bash manual:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

(...)

Simple Command Expansion

If no command name results [from command expansion], the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.

Here are the relevant parts of the bash manual:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

(...)

Simple Command Expansion

If no command name results [from command expansion], the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment.

Note: bear in mind that this is not specific to bash, but specified by POSIX.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]
added relevant parts of the bash manual
Source Link
xhienne
  • 18.3k
  • 2
  • 58
  • 71

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.

Here are the relevant parts of the bash manual:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

(...)

Simple Command Expansion

If no command name results [from command expansion], the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.

Here are the relevant parts of the bash manual:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

(...)

Simple Command Expansion

If no command name results [from command expansion], the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]
replacing image with code
Source Link
xhienne
  • 18.3k
  • 2
  • 58
  • 71

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

code sample

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

code sample

This is equivalent to:

( export someVariable=something; command )

This makes someVariable an environment variable, with the assigned value, but only for the command being run.


Edit - Summarized discussion from comments in the answer

The reason BAZ=JAKE echo $BAZ, doesn't print JAKE is because variable substitution is done before anything else. If you by-pass variable substitution, this behaves as expected:

$ echo_baz() { echo "[$BAZ]"; }
$ BAZ=Jake echo_baz
[Jake]
$ echo_baz
[]
Moved conversation in comments into the answer
Source Link
xhienne
  • 18.3k
  • 2
  • 58
  • 71
Loading
Source Link
xhienne
  • 18.3k
  • 2
  • 58
  • 71
Loading