Use export Command in Linux / Unix

by on May 7, 2011 · 1 comment· LAST UPDATED May 7, 2012


How do I use export command under Linux or Unix like operating systems to set variables?

You export shell variables using the following syntax:

export VAR
### Assign value before exporting ##
export VAR=value


export VAR

The export command will marks each VAR for automatic export to the environment of subsequently executed commands i.e. make the local shell variable VAR global. To make the local shell variable called PATH type the following:

### export PATH ###
export PATH=$PATH:/usr/local/bin
echo "$PATH"

Set a new EDITOR variable:

export EDITOR=/usr/bin/vim

You need to add export statements to ~/.bash_profile or ~/.profile or /etc/profile file. This will export variables permanently:
$ vi ~/.bash_profile
Sample file

export PATH
# set vim as a text editor
export EDITOR=/usr/bin/bin
# set colorful prompt 
export PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
# set java_home
export JAVA_HOME=/usr/local/jdk

To see all a list of all exported variables and functions, enter:

export -p

Sample outputs:

declare -x COLORTERM="gnome-terminal"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-pODhldZ1lj,guid=6cfbd17d02f210f6de5e630b00000165"
declare -x DESKTOP_SESSION="gnome"
declare -x DISPLAY=":0.0"
declare -x GDMSESSION="gnome"
declare -x GDM_KEYBOARD_LAYOUT="us"
declare -x GDM_LANG="en_IN"
declare -x GNOME_DESKTOP_SESSION_ID="this-is-deprecated"
declare -x GNOME_KEYRING_CONTROL="/tmp/keyring-oDL07q"
declare -x GNOME_KEYRING_PID="2708"
declare -x GPG_AGENT_INFO="/tmp/seahorse-PBDijt/S.gpg-agent:2777:1"
declare -x GTK_MODULES="canberra-gtk-module"
declare -x GTK_RC_FILES="/etc/gtk/gtkrc:/home/vivek/.gtkrc-1.2-gnome2"
declare -x HOME="/home/vivek"
declare -x LANG="en_IN"
declare -x LANGUAGE="en_IN:en"
declare -x LOGNAME="vivek"
declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:"
declare -x OLDPWD
declare -x ORBIT_SOCKETDIR="/tmp/orbit-vivek"
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
declare -x PWD="/tmp/n"
declare -x SESSION_MANAGER="local/wks01:@/tmp/.ICE-unix/2727,unix/wks01:/tmp/.ICE-unix/2727"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_AGENT_PID="2763"
declare -x SSH_AUTH_SOCK="/tmp/keyring-oDL07q/ssh"
declare -x TERM="xterm"
declare -x USER="vivek"
declare -x USERNAME="vivek"
declare -x WINDOWID="73400323"
declare -x WINDOWPATH="7"
declare -x XAUTHORITY="/var/run/gdm3/auth-for-vivek-DSw31c/database"
declare -x XDG_DATA_DIRS="/usr/share/gnome:/usr/share/gdm/:/usr/local/share/:/usr/share/"
declare -x XDG_SESSION_COOKIE="6cff67927ad82fca095a44640000001f-1336294442.327911-838315070"
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 1 comment… read it below or add one }

1 M P September 28, 2012 at 2:17 pm

Could you comment on the following unexpected (to me) behavior of the export command in bash and dash. Note the punctuation or lack thereof.
($ = prompt, # = output)

standard/normal cases:
$ echo -$bar -$foo
# – –
$ export bar=foo; echo -$bar
# -foo
$ echo -$bar
# -foo
$ bar=fu; export bar; echo -$bar
# -fu
$ echo -$bar
# -fu
$ bar=foo; echo -$bar
# -foo
$ echo -$bar
# -foo

And now for something different:
$ foo=bar echo -$foo
# –
$ foo\=bar ; echo -$foo
# fu=bar: command not found
# –
$ echo -$foo
# –
$ foo=bar export foo; echo -$foo
# -bar
$ foo=bear; export foo; echo -$foo
# -bear
$ `foo=bar` echo -$foo
# -bar

My question is basically, why does the shell appear to be automatically expanding a variable assignment (or “silently executing an assignment”) prior to an export, but not prior to another command like echo? Is export “special”? What other circumstances have behavior like export?

The reason this came up is because I’ve seen a couple of example scripts that have a line similar to
/bin/sh -c ‘DISPLAY=:0 some_GUI_program’
Should this do anything in general? If so, why?

(I’ve been using linux on and off for about 15 years now, including at one point administering a production linux box, but still feel like a complete n00b)


Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: