Environment variables
An environment variable is a named value that programs can read, such as PATH or HOME. A normal shell variable exists only in the current shell. export marks a variable so that every program started from this shell can also see it.
Basic use
export variable_name=value
Example:
export MY_NAME="John"

Do not put spaces around =.
Read the variable
echo $MY_NAME

Export a function (Bash)
function name() {
echo "Hello, $MY_NAME!"
}
export -f name
View all exported variables
export

You can also use export -p or printenv.
Remove a variable
unset MY_NAME

Common uses
- Add a folder to
PATH, the list of folders searched for commands:
export PATH=$PATH:/path/to/bin
- Set language and locale:
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
- Give settings to scripts:
export DATABASE_HOST=localhost
export DATABASE_USER=myuser
- Set a shell alias. This is not an
export, but it is often put in the same file:
alias ll='ls -la'
Make it permanent
An exported variable lasts only for the current session. To keep it, add the line to a startup file:
~/.bashrcfor your interactive shells.~/.profilefor login shells./etc/environmentfor all users (plainNAME=valuelines, noexport).
Then reload the file:
source ~/.bashrc
Quick recap
export NAME=valueshares a variable with child programs.unset NAMEremoves it.- Add it to
~/.bashrcto keep it after logout.