What is the difference between an inline variable assignment and a regular one in Bash?

The format VAR=value command sets the variable VAR to have the value value in the environment of the command command. The spec section covering this is the Simple Commands. Specifically:

Otherwise, the variable assignments shall be exported for the execution environment of the command and shall not affect the current execution environment except as a side-effect of the expansions performed in step 4.

The format VAR=value; command sets the shell variable VAR in the current shell and then runs command as a child process. The child process doesn’t know anything about the variables set in the shell process.

The mechanism by which a process exports (hint hint) a variable to be seen by child processes is by setting them in its environment before running the child process. The shell built-in which does this is export. This is why you often see export VAR=value and VAR=value; export VAR.

The syntax you are discussing is a short-form for something akin to:

VAR=value
export VAR
command
unset -v VAR

only without using the current process environment at all.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)