Defining a variable with or without export
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.
What does export variable without an equals sign do?
The two lines of code (better to use double brackets):
[[ -r /etc/java/java.conf ]] && . /etc/java/java.conf
export JAVA_HOME
...equates to this:
-r
checks if /etc/java/java.conf
exists and read permission is granted.
&&
if the condition above is true then source the file.
export JAVA_HOME
takes the previously-assigned value of $JAVA_HOME
from the sourced file making it available to subprocesses rather than only within the shell.
- What does "export" do in shell programming?
- What does "source" do?
- Conditional statements in bash
Exporting a function local variable to the environment
export
doesn't copy values in to the current environment. Instead, it sets the export attribute on a name. When a new processes is started, any variables marked with that attribute are copied (along with their current values) into the environment of the new process.
When t_export
returns, the variable dummy
goes out of scope, which means it is no longer available to be exported to new processes.
Do you need to include export for environment variables in bash profile / zshrc?
So is there a difference when prefixing the environment variable declaration with export inside the .zshrc file?
Yes, one is an environment variable and the other isn't.
The difference doesn't matter (much) to your shell, but to processes started by your shell. Environment variables are inherited by child processes, regular shell variables are not.
~ % foo=3; printenv foo
~ % export foo=3; printenv foo
3
In the first case, printenv
has no variable named foo
in its environment; in the second case, it does.
BASH: difference between export k=1 vs. k=1
Every process, even on Windows, has a block of memory known as the environment block, this contains environment variables. When a new process is created, by default, the environment block of the parent process is copied to the child, so environment variables are a simple way of passing text data to a child process.
The export
command creates an environment variable, or converts an ordinary local variable into an environment variable. In the C-shell, one of its few redeeming features is that it uses a different syntax for environment variables (setenv
) to local variables (set
). Bourne shell derivatives, like Bash and Korn shell, hide all that.
Currently, only simple values can be passed, so items like arrays are not supported (it just exports the first element). Variable attributes, set using define
, are also not exported unless the child process is a shell of the same type, i.e. another instance of bash. This also applies to exported functions, although it is possible to sometimes hack this between shells of different types (using eval
).
In Bash (and others) there is a shell setting called allexport
which means all variables are environment variables - probably a bad idea to se generally. You can supply a different environemnt block from languages like C using execve
, but from the shell you need a program like env
, see man env
for details.
Accessing Linux environment variables without assigned value
Shell has a notion of internal and exported environment variables.
A shell command like ABC=xyz
sets an internal variable (which can be seen in Bash using set
).
To export an internal variable, there's the export
command.
export ABC=xyz
will create an internal variable and export it (exported ones can be seen using env
).
export ABC
by itself does not define any variable, it simple tags ABC
to be exported, when it's defined.
Try this for example:
$ ABC=xyz
$ set | grep ABC
ABC=xyz
$ env | grep ABC
And now with export
:
$ export ABC
$ set | grep ABC
$ env | grep ABC
$ ABC=xyz
$ set | grep ABC
ABC=xyz
$ env | grep ABC
ABC=xyz
Notice how the variable got exported the moment it was set. So export ABC
is purely a shell feature, it does not modify the environment. There is no API for it.
To set an environment variable with an empty value, use export ABC=""
:
$ export ABC=""
$ env | grep ABC
ABC=
What does export do in shell programming?
Exported variables such as $HOME
and $PATH
are available to (inherited by) other programs run by the shell that exports them (and the programs run by those other programs, and so on) as environment variables. Regular (non-exported) variables are not available to other programs.
$ env | grep '^variable='
$ # No environment variable called variable
$ variable=Hello # Create local (non-exported) variable with value
$ env | grep '^variable='
$ # Still no environment variable called variable
$ export variable # Mark variable for export to child processes
$ env | grep '^variable='
variable=Hello
$
$ export other_variable=Goodbye # create and initialize exported variable
$ env | grep '^other_variable='
other_variable=Goodbye
$
For more information, see the entry for the export
builtin in the GNU Bash manual, and also the sections on command execution environment and environment.
Note that non-exported variables will be available to subshells run via ( ... )
and similar notations because those subshells are direct clones of the main shell:
$ othervar=present
$ (echo $othervar; echo $variable; variable=elephant; echo $variable)
present
Hello
elephant
$ echo $variable
Hello
$
The subshell can change its own copy of any variable, exported or not, and may affect the values seen by the processes it runs, but the subshell's changes cannot affect the variable in the parent shell, of course.
Some information about subshells can be found under command grouping and command execution environment in the Bash manual.
export command with variable name and value from other variable declaration in bash script
Try as below:
name='COMPANY_NAME'
value='System Manager'
command="$name=$value"
echo $command
export "$command"
Notice the change in third and last statement (important)
Explanation: Unquoted expansion treats white space as token to separate.
Related Topics
How to Permanently Set $Path on Linux/Unix
How to Change Permissions For a Folder and Its Subfolders/Files
How to Run Script as Another User Without Password
What Is an Uninterruptible Process
How to Redirect Output of Systemd Service to a File
What Is the Maximum Size of a Linux Environment Variable Value
How to Redirect Output of an Already Running Process
How to Disassemble Raw 16-Bit X86 Machine Code
How to Redirect Stderr and Stdout to Different Files in the Same Line in Script
How to Convert Dos/Windows Newline (Crlf) to Unix Newline (Lf)
Redirect All Output to File in Bash
How to Automatically Redirect Http to Https on Apache Servers
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat
How to Find the Last Field Using 'Cut'
The Bash Command :(){ :|:& };: Will Spawn Processes to Kernel Death. Can You Explain the Syntax