Linux CLI 23 ๐ง printenv and export commands
Environment variables are key-value pairs that the shell and its child processes use to store information โ your username, home directory, default editor, and much more. The printenv and export commands let you view and set these variables.
What Are Environment Variables?
Environment variables are dynamic values that affect how processes run.
| Variable | Description |
|---|---|
HOME | User’s home directory |
USER | Current username |
PATH | Directories the shell searches for commands |
PWD | Present working directory |
LANG | Default language setting |
HOSTNAME | Name of the computer |
DISPLAY | X display (:0 = first display) |
SHELL | Current shell program |
EDITOR | Default text editor |
TERM | Terminal type |
Key point: Environment variables are inherited by child processes โ that’s why they’re called “environment.”
Viewing Variables โ printenv
printenv USER
printenv DISPLAY
printenv LANG
printenv PATH
printenv SHELL
printenv PWD
| Command | Description |
|---|---|
printenv | Show all environment variables |
printenv USER | Show a specific variable |
printenv DISPLAY | Show X display |
printenv PATH | Show command search path |
Examples
Show all variables:
$ printenv
SHELL=/bin/bash
USER=kronos
HOME=/home/kronos
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/kronos
LANG=en_US.UTF-8
HOSTNAME=olympos
DISPLAY=:0
TERM=xterm-256color
...
Specific variables:
$ printenv USER
kronos
$ printenv HOME
/home/kronos
$ printenv PATH
/usr/local/bin:/usr/bin:/bin
$ printenv SHELL
/bin/bash
$ printenv PWD
/home/kronos
$ printenv DISPLAY
:0
$ printenv LANG
en_US.UTF-8
$ printenv HOSTNAME
olympos
The echo $VAR Alternative
You can also view a variable with echo:
$ echo $USER
kronos
$ echo $HOME
/home/kronos
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ echo $USER $HOME
kronos /home/kronos
$ echo "User: $USER, Home: $HOME"
User: kronos, Home: /home/kronos
echo vs printenv:
| Aspect | echo $VAR | printenv VAR |
|---|---|---|
| Speed | Faster | Slightly slower |
| Formatting | Can mix with text | Prints raw value |
| Exit code | Always 0 | Non-zero if not found |
| Use case | In scripts | Checking if variable exists |
Best practice:
# For display โ either works
echo $USER
printenv USER
# For scripting โ use printenv to check existence
if printenv MY_VAR > /dev/null; then
echo "MY_VAR is set"
fi
Common Environment Variables
| Variable | Purpose | Example Value |
|---|---|---|
HOME | Home directory | /home/kronos |
USER | Username | kronos |
PATH | Command search path | /usr/bin:/bin |
PWD | Present working directory | /home/kronos |
LANG | Language/locale | en_US.UTF-8 |
HOSTNAME | Machine name | olympos |
DISPLAY | X display | :0 |
SHELL | Current shell | /bin/bash |
EDITOR | Default text editor | vim or nano |
TERM | Terminal type | xterm-256color |
PS1 | Shell prompt format | \u@\h:\w\$ |
OLDPWD | Previous directory | /var/log |
LOGNAME | Login name | kronos |
UID | User ID | 1000 |
Setting Variables โ export
export test="kronos"
echo $test
printenv test
| Command | Description |
|---|---|
export VAR=value | Set and export a variable |
export VAR | Mark existing variable for export |
export -p | List all exported variables |
export -f func | Export a function |
export -n VAR | Remove export flag |
Temporary Variables
Local variable (not exported):
$ test="kronos"
$ echo $test
kronos
$ bash # Open a new shell
$ echo $test
# (empty โ not inherited!)
$ exit
Exported variable (inherited by children):
$ export test="kronos"
$ echo $test
kronos
$ bash # Open a new shell
$ echo $test
kronos # โ
Inherited!
$ exit
Key difference:
| Type | Command | Inherited by child processes? |
|---|---|---|
| Shell variable | VAR=value | โ No |
| Environment variable | export VAR=value | โ Yes |
Verifying Export
$ export test="kronos"
$ echo $test
kronos
$ printenv test
kronos
$ env | grep test
test=kronos
The set Command
Without arguments, set prints all variables โ including shell variables and functions:
$ set
BASH=/bin/bash
BASH_VERSION=5.1.16
HOME=/home/kronos
HOSTNAME=olympos
...
With arguments, set is used to configure shell options:
$ set -e # Exit on error
$ set -x # Debug mode (print each command)
$ set +x # Disable debug mode
set vs export:
| Command | Purpose |
|---|---|
set | Show all variables (shell + environment + functions) |
export | Show only environment variables |
export -p | Show only exported variables |
Examples:
# All variables
$ set | head -20
# Only environment variables
$ export -p
# Environment variables (alternative)
$ env
Exporting Functions
$ myfunc() {
echo "Hello from myfunc!"
}
$ export -f myfunc
$ bash
$ myfunc
Hello from myfunc!
$ exit
Use case: Pass helper functions to sub-shells or scripts.
Making Variables Permanent
echo "export VAR_NAME='value'" >> ~/.bashrc
source ~/.bashrc
Step-by-step:
1. Add to ~/.bashrc:
$ echo "export VAR_NAME='value'" >> ~/.bashrc
2. Reload the config:
$ source ~/.bashrc
3. Verify:
$ echo $VAR_NAME
value
Example โ Setting a Permanent Variable
# Set EDITOR permanently
$ echo "export EDITOR='nano'" >> ~/.bashrc
$ source ~/.bashrc
$ echo $EDITOR
nano
# Now any program using $EDITOR will use nano
$ crontab -e # Opens in nano
More examples:
# Add a custom directory to PATH
echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc
source ~/.bashrc
# Set default language
echo "export LANG='en_US.UTF-8'" >> ~/.bashrc
# Set Java home
echo "export JAVA_HOME='/usr/lib/jvm/java-17-openjdk'" >> ~/.bashrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Complete Example Session
# ============================================
# PART 1: VIEWING VARIABLES WITH PRINTENV
# ============================================
$ printenv USER
kronos
$ printenv HOME
/home/kronos
$ printenv PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin
$ printenv SHELL
/bin/bash
$ printenv PWD
/home/kronos
$ printenv LANG
en_US.UTF-8
$ printenv HOSTNAME
olympos
$ printenv DISPLAY
:0
# All variables
$ printenv | head -15
SHELL=/bin/bash
PWD=/home/kronos
LOGNAME=kronos
HOME=/home/kronos
LANG=en_US.UTF-8
...
# ============================================
# PART 2: VIEWING WITH ECHO
# ============================================
$ echo $USER
kronos
$ echo $HOME
/home/kronos
$ echo "User: $USER, Shell: $SHELL"
User: kronos, Shell: /bin/bash
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
# ============================================
# PART 3: SETTING TEMPORARY VARIABLES
# ============================================
# Shell variable (not exported)
$ test="kronos"
$ echo $test
kronos
$ bash
$ echo $test
# (empty โ not inherited)
$ exit
# Environment variable (exported)
$ export test="kronos"
$ echo $test
kronos
$ printenv test
kronos
$ bash
$ echo $test
kronos # โ
Inherited!
$ exit
# ============================================
# PART 4: LISTING EXPORTED VARIABLES
# ============================================
$ export -p
declare -x HOME="/home/kronos"
declare -x LANG="en_US.UTF-8"
declare -x PATH="/usr/local/bin:/usr/bin:/bin"
declare -x SHELL="/bin/bash"
declare -x USER="kronos"
...
$ env | head -10
SHELL=/bin/bash
PWD=/home/kronos
LOGNAME=kronos
HOME=/home/kronos
...
# ============================================
# PART 5: SET COMMAND
# ============================================
# Show all variables
$ set | head -20
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:...
BASH_VERSION=5.1.16(1)
...
# Enable debug mode
$ set -x
+ echo hello
hello
$ set +x
# Disable debug mode
# ============================================
# PART 6: MAKING VARIABLES PERMANENT
# ============================================
# Add to ~/.bashrc
$ echo "export EDITOR='nano'" >> ~/.bashrc
$ echo "export MY_NAME='kronos'" >> ~/.bashrc
# Reload
$ source ~/.bashrc
# Verify
$ echo $EDITOR
nano
$ echo $MY_NAME
kronos
# ============================================
# PART 7: PRACTICAL EXAMPLES
# ============================================
# Custom command path
$ mkdir -p ~/bin
$ echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc
$ source ~/.bashrc
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/home/kronos/bin
# Set Java environment
$ echo 'export JAVA_HOME="/usr/lib/jvm/java-17-openjdk"' >> ~/.bashrc
$ echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc
$ java -version
# Development environment
$ echo "export NODE_ENV='development'" >> ~/.bashrc
$ echo "export DEBUG=true" >> ~/.bashrc
$ source ~/.bashrc
# ============================================
# PART 8: EXPORT FUNCTIONS
# ============================================
$ greet() {
echo "Hello, $USER!"
}
$ export -f greet
$ bash
$ greet
Hello, kronos!
$ exit
# ============================================
# PART 9: UNEXPORT AND UNSET
# ============================================
# Remove export flag (keeps variable)
$ export -n test
$ echo $test # Still works
kronos
$ bash
$ echo $test # Not inherited
# (empty)
$ exit
# Remove variable entirely
$ unset test
$ echo $test
# (empty)
Quick Reference
Viewing Variables
| Command | Description |
|---|---|
printenv | Show all environment variables |
printenv VAR | Show specific variable |
echo $VAR | Show variable (quick) |
env | Show all env variables |
set | Show all variables + functions |
export -p | Show only exported variables |
Setting Variables
| Command | Description |
|---|---|
VAR=value | Shell variable (not exported) |
export VAR=value | Environment variable (exported) |
export -f func | Export a function |
export -n VAR | Remove export flag |
unset VAR | Remove variable entirely |
Common Variables
| Variable | Meaning |
|---|---|
HOME | Home directory |
USER | Username |
PATH | Command search path |
PWD | Current directory |
SHELL | Current shell |
LANG | Language setting |
HOSTNAME | Machine name |
EDITOR | Default editor |
DISPLAY | X display |
TERM | Terminal type |
Best Practices
โ Do This:
# Check a variable before using it
if [ -z "$EDITOR" ]; then
export EDITOR=nano
fi
# Use quotes when displaying variables
echo "$HOME" # โ
echo "$USER is logged in"
# Add to PATH correctly
export PATH="$PATH:$HOME/bin" # โ
Append, don't replace
# Make permanent in ~/.bashrc
echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc
# Use printenv to check existence
if printenv MY_VAR > /dev/null 2>&1; then
echo "MY_VAR is set"
fi
# Export only what's needed
export PROJECT_HOME="/home/kronos/projects"
โ Don’t Do This:
# Don't forget to export when you need inheritance
test="value" # โ Not inherited by children
export test="value" # โ
# Don't replace PATH โ always append
export PATH="/home/kronos/bin" # โ Breaks commands!
export PATH="$PATH:/home/kronos/bin" # โ
# Don't store secrets in ~/.bashrc
export PASSWORD="secret123" # โ Insecure!
# Don't forget to source after editing
nano ~/.bashrc
# ... add export ...
source ~/.bashrc # โ
Required
# Don't use single quotes when you need expansion
echo 'User: $USER' # โ Shows $USER literally
echo "User: $USER" # โ
Expands
# Don't overwrite important variables
export HOME="/tmp" # โ Breaks everything!
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
Forgot export | Variable not inherited | Add export |
| Replaced PATH | Commands not found | Use $PATH: prefix |
'$VAR' in quotes | Literal $VAR shown | Use "$VAR" |
Edited .bashrc without source | Changes not applied | source ~/.bashrc |
| Variable lost after reboot | Not saved | Add to ~/.bashrc |
Spaces around = | Error | VAR=value (no spaces) |
Real-World Examples
1. Add Custom Scripts Directory
# Create the directory
$ mkdir -p ~/bin
# Add to PATH permanently
$ echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc
$ source ~/.bashrc
# Now any script in ~/bin can be run from anywhere
$ cp myscript.sh ~/bin/
$ chmod +x ~/bin/myscript.sh
$ myscript.sh # โ
Works from anywhere
2. Set Default Editor
# Set nano as default editor
$ echo 'export EDITOR=nano' >> ~/.bashrc
$ source ~/.bashrc
# Now uses nano
$ crontab -e # Opens in nano
$ git commit # Uses nano for commit message
$ visudo # Uses nano
3. Development Environment Variables
# Add to ~/.bashrc
export NODE_ENV=development
export DATABASE_URL="postgres://localhost:5432/myapp"
export API_KEY="dev-key-12345"
export DEBUG=true
source ~/.bashrc
4. Java Development Setup
# Add to ~/.bashrc
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk"
export PATH="$JAVA_HOME/bin:$PATH"
export MAVEN_HOME="/opt/maven"
export PATH="$MAVEN_HOME/bin:$PATH"
source ~/.bashrc
$ java -version
openjdk version "17.0.2"
5. Display Variable in Shell Prompt
# Add to ~/.bashrc
export PS1='[\u@\h \W]\$ '
# Result: [kronos@olympos ~]$
6. Temporary Override for a Command
# Change locale just for one command
$ LANG=es_ES.UTF-8 date
jue 15 ene 2024 10:35:22 UTC
# Back to normal
$ date
Mon Jan 15 10:35:25 UTC 2024
7. Conditional Variable
# In a script
if [ -z "$DEPLOY_ENV" ]; then
export DEPLOY_ENV="staging"
fi
echo "Deploying to: $DEPLOY_ENV"
8. Show What’s Changed
# Compare current env with defaults
$ printenv | sort > current-env.txt
$ diff /etc/environment current-env.txt
Visual: Variable Inheritance
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Parent Shell (bash) โ
โ โ
โ VAR="value" โ Shell variable โ
โ export EVAR="value" โ Environment variable โ
โ โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ forks
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Child Process (bash) โ
โ โ
โ echo $VAR โ (empty!) โ
โ echo $EVAR โ value โ
โ
โ โ
โ Only EVAR is inherited! โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Command | Purpose | Example |
|---|---|---|
printenv | View env variables | printenv USER |
echo $VAR | Quick view | echo $HOME |
export VAR=value | Set env variable | export EDITOR=nano |
export -p | List exported | export -p |
export -n VAR | Remove export flag | export -n test |
unset VAR | Delete variable | unset test |
set | Show all variables | set | head |
env | Show env variables | env |
Key takeaways:
- Environment variables are key-value pairs inherited by child processes
printenvshows all env variables โprintenv VARshows oneecho $VARis a quick way to view โ great in scripts- Shell variables (
VAR=x) are not inherited โ environment variables (export VAR=x) are export -plists only exported variablesexport -f funcexports a function to sub-shellsexport -n VARremoves the export flag (keeps variable)unset VARremoves the variable entirely- Permanent variables go in
~/.bashrcโ thensource ~/.bashrc - Never replace
PATHโ always append with$PATH:newdir - Common variables:
HOME,USER,PATH,PWD,SHELL,LANG,EDITOR,DISPLAY
Remember: The key distinction is between shell variables (local to the current shell) and environment variables (inherited by child processes). Use export to make a variable available everywhere. Use printenv to view environment variables and echo $VAR for quick checks. For permanent changes, add to ~/.bashrc and remember to source it. And when modifying PATH, always use $PATH:newdir โ never replace the whole thing!
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!