Sobes.tech
Senior

What is the difference between the variables VAR=value and export VAR=value in Bash?

sobes.tech AI

Answer from AI

A variable declared as VAR=value in Bash exists only in the current shell and is accessible only within it. It is not passed to child processes.

However, if you declare a variable using export VAR=value, it becomes an exported variable — meaning it is accessible not only in the current shell but also in all child processes launched from this shell.

Example:

VAR=123
bash -c 'echo $VAR'  # Will output an empty string, as the variable is not accessible in the child shell

export VAR=123
bash -c 'echo $VAR'  # Will output 123, as the variable is exported and accessible in the child shell

Thus, export is used to pass environment variables to child processes.