This article explains the basics of Linux export command and shows how to set, view and manipulate Linux env variables using export command.
Environment variables in Linux are used by most of the activities taking place on a Linux system. From executing a standard command like ‘ls’ to installing a new software in your Linux box, each activity either uses or alters the environment variable list.
Environment variable definition:
Its a named object that can be used by multiple applications as it contains some valuable information required by these applications
1. View all the Current Exported Variables
Use export -p to view all the env variables as shown below. Partial output is shown here.
$ export -p declare -x COLORTERM="gnome-terminal" declare -x DEFAULTS_PATH="/usr/share/gconf/gnome.default.path" declare -x DESKTOP_SESSION="gnome" declare -x HOME="/home/himanshu" declare -x LOGNAME="himanshu" declare -x MANDATORY_PATH="/usr/share/gconf/gnome.mandatory.path" declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" declare -x PWD="/home/himanshu" declare -x SHELL="/bin/bash" declare -x SSH_AGENT_PID="1663" declare -x USER="himanshu" declare -x USERNAME="himanshu" declare -x WINDOWID="56623107" ..
Note that you can also see this list using the ‘env’ command.
2. View a Specific Exported Variable
Use echo command to display a specific environment variable. The following example displays the value of the PATH env variable.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Also, refer to 15 Useful Bash Shell Built-in Commands, which explains about few other commands (e.g. env, unset, etc.) that also works on env variables.
3. Set an Environment Variable
You can add a new environment variable as shown below. The following creates a new environment variable called “MYAPP” and assigns the value of 1.
$ export MYAPP=1
Verify that the environment variable is set properly using the echo command.
$ echo $MYAPP 1
Note: Don’t give a space before and/or after = sign. For example, all of the following are invalid.
$ export MYAPP = 1 -bash: export: `=': not a valid identifier -bash: export: `1': not a valid identifier $ export MYAPP =1 -bash: export: `=1': not a valid identifier $ export MYAPP= 1 -bash: export: `1': not a valid identifier
4. Append a Value to an Environment Variable
In the below example we try to append a new path to PATH variable. Use ‘:’ to separate the values.
$ export PATH=$PATH:/home/himanshu/practice/
Verify that the value got appendd properly.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/himanshu/practice/
5. Variables Without Export
Assign a variable with a value in an interactive shell, and try to access the same in your shell script.
$ MYAPP=1 $ cat myapp.sh #!/bin/bash echo "MYAPP=$MYAPP" MYAPP=2 echo "MYAPP=$MYAPP"
Now, execute the above script as shown below.
$ ./myapp.sh MYAPP= MYAPP=2
Still you will get blank value for variable MYAPP. The shell stores variable MYAPP with the LINUX only in the current shell. During the execution of myapp.sh, it spawns the shell and it executes the script. So the variable MYAPP will not have the value in the spawned shell. You need to export the variable for it to be inherited by another program – including a shell script.
Also, refer to the bash variable tutorial, which explains local and global scope of variables, declaring a bash variable, and few other examples.
6. Exporting Variables Permanently
To export variables permanently, you can add the export command in any of the following start-up files :
~/.profile ~/.bash_profile /etc/profile
There could be some limitations like ” ~/.profile is not read by bash, if ~/.bash_profile or ~/.bash_login exists.”. So one should read the ‘/usr/share/doc/bash/examples/startup-files’ to get a better idea of how these start-up files work.
It is also important to understand the execution sequence of .bash_profile, .bashrc, .bash_login, .profile and .bash_logout, which will help you to decide which file to use to set your environment variable for your specific situation.