Variables concatenated with & are not defined in subsequent commands

I would like to know why the following shows an empty dialog box:

testvar="hello" & zenity --info --text "${testvar}"

While this works:

testvar="hello"; zenity --info --text "${testvar}"
Asked By: robertspierre


The & does not concatenate commands. It runs the preceding command in a subshell in the background and immediately returns control to the foreground shell.

Given a variation on your scenario:

testvar="hello" & echo "testvar=$testvar"

the assignment is run in a subshell and therefore cannot affect the parent, which continues immediately with the next command, echo.

The semicolon, ;, is a command separator and can be considered equivalent to a newline.

For more interest consider these further variations:

a=apple; a=avocado & echo "a=$a"

a=apple; ( a=avocado; echo "## a=$a" ) & sleep 2; echo "a=$a"

Finally just a nitpick to point out that although these are variables, none of them are necessarily environment variables. (You can list those with env.)

Answered By: Chris Davies
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.