Junior — Middle
Explain the differences between the keywords Export and Var when declaring variables in your programming language.
sobes.tech AI
Answer from AI
In the context of programming languages such as Bash or other shell scripts, the keywords export and var serve different functions when declaring variables:
-
varis usually used to declare a local variable in the current context or function. A variable declared withvaris only accessible within the current scope. -
exportis used to declare an environment variable, which becomes available not only in the current process but also in all child processes. This means that a variable withexportis passed into the environment of other programs.
Example in Bash:
var=123 # local variable
export PATH=/usr/bin:$PATH # environment variable accessible to child processes
Thus, export extends the scope of the variable to child processes, while var restricts it to the current context.