Linux Export Command Examples (How to Set Environment Variables)

by Himanshu Arora on July 16, 2012

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.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 7 comments… read them below or add one }

1 Jalal Hajigholamali July 16, 2012 at 10:23 am

Hi,

Thanks a lot…

i think “set MYAPP=1″ must be changed by “MYAPP=1″

2 Russell July 16, 2012 at 10:52 am

I think Jalal is correct, here is my output (Linux Mint 13)
~ $ export MYAPP=1
~ $ echo $MYAPP
1
~ $ set MYAPP=2
~ $ echo $MYAPP
1
~ $ MYAPP=2
~ $ echo $MYAPP
2

Also opening a new shell still had the new variable
so your point number 5 seems wrong:

~ $ bash
~ $ echo $MYAPP
2
~ $

3 rassko July 16, 2012 at 12:16 pm

To add variables really permanent in Solaris, crle is better
crle -u -l /usr/lib:/lib: …

4 Simen July 16, 2012 at 10:01 pm

To Russell,
if export a variable, then the changes in parent shell will be inherited by child shell.
In your case, the variable MYAPP is exported, so you will get value 2 in child shell

5 Ramesh Natarajan July 16, 2012 at 10:52 pm

@Jalal, Russell,

Thanks for pointing out the issue. Point number 5 is now changed.

6 Raul January 10, 2014 at 11:29 am

Very clear Ramesh, thanks!

7 Negar April 3, 2014 at 12:56 am

Hei, I am quite new to the shell programming, could you explain to me what does this mean?

export BINDIR=~/bin

Leave a Comment

Previous post:

Next post: