Two places a value can live
When you type a line such as greeting=hello at the prompt, bash stores that value in its own memory. The name greeting is now a shell variable. Inside bash you can read it with $greeting, use it in an if statement or a loop, and change it at any time. None of the programs that bash starts can see it, because a shell variable is not part of anything that gets passed on.
greeting=hello
echo "$greeting"
env | grep greetingThe first line of output is hello, because bash can read its own variable. The second command prints nothing. env prints the environment, which is the list of NAME=value strings that bash gives to each new program. The name greeting is not on that list, so grep finds no match, prints nothing, and exits with status 1.
The export attribute
The export command marks a variable so that bash copies it into the environment of every program it starts from then on. The value does not have to be set again. export greeting changes only the attribute. The value stays exactly where it was.
export greeting
env | grep greetingNow the second command prints greeting=hello. The declare builtin shows the attribute directly. An unexported variable prints as declare -- greeting="hello", and an exported one has an x flag, as in declare -x greeting="hello". The printenv command is a shortcut for reading the environment alone: printenv greeting prints the value only when the variable is exported, and prints nothing otherwise.
Reading the two lists
set with no arguments prints every shell variable and every function that bash knows about. That is usually far more output than you need. When the question is what a child program will receive, use env or printenv, because they show only the exported variables. If a program behaves as though a setting is missing, the first check is printenv NAME, and the second check is set | grep NAME to see whether the variable exists in the shell at all.
A variable can exist in the shell and be missing from the environment at the same time. That combination is the most common cause of "it works at the prompt but not in my script".
Temporary values for one command
An assignment placed directly in front of a command gives that one command its own environment value. The shell variable itself does not change. Bash removes the temporary value when the command ends.
LANGUAGE_HINT=verbose env | grep LANGUAGE_HINT
echo "[${LANGUAGE_HINT:-unset}]"The first command prints LANGUAGE_HINT=verbose, because env received the value. The second command prints [unset], because the shell never stored the value. This pattern is useful when you want to change one program's behavior without changing anything else.
Removing a value or the export mark
unset name removes the variable from the shell entirely. export -n name is different: it keeps the value in the shell but removes the export attribute, so later programs stop receiving it. This is handy when you want to test a program with and without a setting while keeping the shell variable around for later use.
Why the split exists
The split is older than Linux and is shared by every POSIX shell. A shell needs private variables for its own logic, such as a loop counter or a path it is building, and it would be wasteful and risky to pass all of them to every program. Programs need a much smaller and more stable set of values, such as where the home directory is, which editor to use, and where to find commands. The environment is that smaller set. Exporting is how you say that a particular name belongs in it.
This also explains why a variable can look correct at the prompt and still be absent for a program. The shell is not hiding anything from you. It is doing what the design asks, and the program sees only the list it was given.
A worked example
Suppose a script named build.sh uses a variable called BUILD_DIR, and you set it at the prompt before running the script. Whether the script sees the value depends on one word. Without export, the script finds the variable unset and falls back to its default. With export, the script uses the directory you chose. The script does not change. Only the attribute of the variable at the prompt changed.
When you are unsure what a process received, do not guess from the shell. Ask the program. The export -p command lists every variable the shell would pass on, in a form that can be read back by bash, and printenv with no arguments prints the same information as plain lines. Comparing either list with the variables you think you exported usually reveals the gap in a few seconds.
When a value should reach one run and nothing else, the prefix form is the better tool, because the shell is left exactly as it was. Choosing between plain assignment, export, and prefix assignment is mostly a question of how long the value should live and who should be able to see it. Plain assignment lives in the shell, export lives in every later program, and a prefix lives for one command.
Common mistakes
Three mistakes account for most of the confusion. The first is setting a value in the shell and then running a script that expects it, without exporting it first. The second is writing spaces around the equals sign. In name = value bash treats name as a command and = and value as arguments to it. The third is assuming that a name in one case is the same as the name in another case. Variable names are case sensitive, so Path and PATH are different variables, and only PATH affects command lookup.