Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a variable with or without export

Tags:

linux

bash

shell

What is export for?

What is the difference between:

export name=value 

and

name=value 
like image 388
flybywire Avatar asked Jul 21 '09 09:07

flybywire


People also ask

What does exporting a variable do?

Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments.

What does it mean to export an environment variable?

"Exporting" a variable in the shell makes it available to all subshells and processes created by that shell. It does not make it available everywhere in the system, only by processes created from that shell.

What is export variable in shell script?

A local shell variable is a variable known only to the shell that created it. If you start a new shell, the old shell's variables are unknown to it. If you want the new shells that you open to use the variables from an old shell, export the variables to make them global.

Does export set environment variable?

Export Environment Variable Export is a built-in shell command for Bash that is used to export an environment variable to allow new child processes to inherit it. To export a environment variable you run the export command while setting the variable.


1 Answers

export makes the variable available to sub-processes.

That is,

export name=value 

means that the variable name is available to any process you run from that shell process. If you want a process to make use of this variable, use export, and run the process from that shell.

name=value 

means the variable scope is restricted to the shell, and is not available to any other process. You would use this for (say) loop variables, temporary variables etc.

It's important to note that exporting a variable doesn't make it available to parent processes. That is, specifying and exporting a variable in a spawned process doesn't make it available in the process that launched it.

like image 172
Brian Agnew Avatar answered Oct 01 '22 14:10

Brian Agnew