Linux Environment Variables: How to Set and Use Them
An environment variable is a named value that programs read at runtime, like a home directory path or an API key. You print one with echo $HOME and create one with export VAR=value. This guide shows how they work and how to make them permanent.
Print an existing variable
Use echo with a dollar sign before the name. Common variables include $HOME, $USER, $PATH and $SHELL.
Set a variable
For the current command only, prefix the command: LANG=en_US.UTF-8 command. To keep a variable in the shell, assign it; to pass it to child programs, export it.
Shell variable vs environment variable
A plain assignment like FOO=bar creates a shell variable visible to the current shell. Only exported variables reach programs you launch. Make a variable available to children with export: export FOO=bar.
Make variables permanent in ~/.bashrc
Variables set in a terminal vanish when it closes. To keep them, add the export line to ~/.bashrc, which bash reads at every interactive start. Use nano to append the line, then run source ~/.bashrc to apply it now.
List and remove variables
env prints all exported variables, and unset VAR removes one. Use env to inspect what programs see before debugging a missing config.
For variables that control user access and identity, see user management.
Keep learning
Related guides: How to Reuse Commands with the bash history Feature · Linux User Management: useradd, usermod and passwd · VIM for Beginners: The Essential Guide. Read all tutorials on the Learn Linux blog.