Environment Variables Basics - Configuration and Practical Usage
What You'll Learn
- Check environment variable values with
echo $VARorenv - Define your own environment variables with
export - Understand
PATHand fix "command not found" errors on your own - Persist settings across sessions using
.bashrc
Target Audience: You know basic commands (pwd, cd, ls) but feel uncertain about what $HOME or export actually means.
Introduction: What Are Environment Variables?
$HOME and $PATH in tutorials. What are those? The $ sign feels like a magic spell I don't understand./home/lina" under the name HOME. So when you write $HOME, the shell looks up the note and replaces it with /home/lina.Quick Summary
- Just check a value →
echo $VARorenv | grep VAR - Use it only in your current shell →
export VAR=value - Keep it for next login → write it in
~/.bashrc - 80% of "command not found" errors are PATH-related
Prerequisites
- OS: Ubuntu / other Linux distributions
- Shell: bash (zsh works almost the same)
- A terminal showing the
$prompt
1. Reading Environment Variables
1-1. Show one variable: echo $VAR
$ echo $HOME
/home/lina
$ echo $USER
lina
Key Points:
$means "expand this variable's value"- Without
$,echo HOMEjust prints the literal text "HOME" - Variable names are uppercase by convention (not required, but it's the norm)
$ makes a huge difference!$ is the switch that pulls out the value."1-2. Show all variables: env / printenv
$ env
SHELL=/bin/bash USER=lina HOME=/home/lina PATH=/usr/local/bin:/usr/bin:/bin LANG=en_US.UTF-8 PWD=/home/lina ...
If the output is long, use env | less to paginate, or env | grep PATH to filter.
1-3. Common variables to know
| Variable | Meaning | Example |
|---|---|---|
HOME |
Your home directory | /home/lina |
USER |
Current username | lina |
PATH |
Directories searched for commands | /usr/local/bin:/usr/bin:/bin |
SHELL |
Your login shell | /bin/bash |
LANG |
Language and locale | en_US.UTF-8 |
PWD |
Current directory (same as pwd) |
/home/lina/work |
HOME and PATH really matter. The rest is just "good to know it exists."2. Setting Variables: Assignment vs export
2-1. Plain assignment (shell variable)
$ MY_NAME=lina $ echo $MY_NAME
lina
No spaces around =. Writing MY_NAME = lina makes the shell think "run a command called MY_NAME," which fails. This is the most common beginner accident.
2-2. Promote to "environment variable" with export
$ MY_NAME=lina $ export MY_NAME
Or in one shot:
$ export MY_NAME=lina
export? They look the same to me.export and the variable is "exported" to children too.2-3. See the difference yourself
$ MY_VAR=hello $ bash -c 'echo $MY_VAR'
(blank — nothing printed)
$ export MY_VAR=hello $ bash -c 'echo $MY_VAR'
hello
Rule of thumb: If a script or another command needs the value, always use export. For a personal scratchpad, plain = is fine.
2-4. Delete a variable
$ unset MY_VAR $ echo $MY_VAR
unset removes the variable from the current shell only. To delete a value defined in .bashrc, edit the file itself.
3. PATH: The Most Important Variable
PATH is exactly how you fix "command not found."3-1. Inspect PATH
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Key Points:
- It's a list of directories separated by
: - When you type a command, the shell searches these directories left to right
- The first match wins
3-2. Find where a command lives: which
$ which ls
/usr/bin/ls
$ which python3
/usr/bin/python3
ls actually lives in /usr/bin/!ls is because /usr/bin is in your PATH. If you removed it, you'd have to type the full path /usr/bin/ls every time.3-3. Add a personal directory to PATH
To use a ~/bin folder for your own scripts:
$ mkdir -p ~/bin $ export PATH="$HOME/bin:$PATH"
Always append $PATH at the end. Writing export PATH="$HOME/bin" alone wipes out the existing PATH — almost every command will then report "command not found." This is the #1 PATH accident.
3-4. Order changes behavior
# Prefer my own version $ export PATH="$HOME/bin:$PATH" # Prefer the system version (mine as fallback) $ export PATH="$PATH:$HOME/bin"
Pro tip: If PATH changes break something, run which command-name to see exactly which binary is being picked up.
4. Persistence: Surviving the Next Login
export, but when I closed the terminal it disappeared...export only lives for the current shell session. The next shell forgets it. To keep it, write it into a config file.4-1. Which file should I edit?
| File | When it loads | Typical use |
|---|---|---|
~/.bashrc |
Every interactive bash shell | Personal vars and aliases |
~/.profile / ~/.bash_profile |
Once at login | Env vars on SSH login |
/etc/environment |
System-wide, all users | Shared system settings (root) |
When in doubt, edit ~/.bashrc. It's the most reliable choice for GUI terminals, WSL, and SSH sessions.
4-2. Example .bashrc additions
Append to the bottom:
# Personal scripts directory export PATH="$HOME/bin:$PATH" # Locale export LANG=en_US.UTF-8 # Default editor export EDITOR=vim
4-3. Reload immediately with source
$ source ~/.bashrc
Or the dot shortcut:
$ . ~/.bashrc
Forgetting source is a classic beginner trap. After editing a config file, always source it or open a new shell.
source actually do?export inside it dies with the child. source runs the lines in your current shell, so the changes stick.5. Common Pitfalls
5-1. Forgetting / adding $
# Wrong: prints the literal string "HOME" $ echo HOME # Right: expands the value $ echo $HOME
5-2. Spaces around =
# Wrong: parsed as a command, fails $ MY_VAR = hello # Right $ MY_VAR=hello
5-3. Wiping out PATH
# Disaster: existing PATH is gone $ export PATH="$HOME/bin" # Correct $ export PATH="$HOME/bin:$PATH"
If you accidentally wipe PATH, just open a new terminal. The default PATH comes back. Don't panic-edit .bashrc first.
5-4. Single vs double quotes
$ NAME=lina # Double quotes: $NAME is expanded $ echo "Hello $NAME" Hello lina # Single quotes: literal, no expansion $ echo 'Hello $NAME' Hello $NAME
Rule of thumb: Use double quotes "..." when you want variable expansion. Use single quotes '...' for literal strings.
5-5. "I exported it but my script doesn't see it"
When you run a script with ./script.sh, exported variables from the parent shell are inherited. But a brand-new terminal loads .bashrc fresh — if the variable isn't there, it doesn't exist.
For variables your scripts depend on, add them to .bashrc so they're always there.
6. Hands-On Practice
Exercise 1: Greet yourself with a variable
$ MY_NAME=(your name) $ echo "Hello, $MY_NAME!"
Show hint
Don't put spaces around =. Use double quotes so $MY_NAME gets expanded.
Exercise 2: Create ~/bin and add it to PATH
$ mkdir -p ~/bin $ echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc $ source ~/.bashrc $ echo $PATH
You should see /home/yourname/bin near the front of PATH.
Exercise 3: Run your own command
$ cat > ~/bin/hello << 'EOF' #!/bin/bash echo "Hello from my own script!" EOF $ chmod +x ~/bin/hello $ hello
Hello from my own script!
7. Summary
Copy-paste cheat sheet
# Inspect echo $HOME # Single variable env # All variables env | grep PATH # Filter which commandname # Where does it live? # Set MY_VAR=value # Shell variable (current shell only) export MY_VAR=value # Environment variable (children too) unset MY_VAR # Delete # Add to PATH safely export PATH="$HOME/bin:$PATH" # Always append $PATH # Persist vim ~/.bashrc # Append export lines source ~/.bashrc # Apply now
Don't do this
export PATH="$HOME/bin"(wipes the existing PATH)MY_VAR = value(spaces around=)- Wrapping
$HOMEin single quotes and wondering why it isn't expanded - Editing
.bashrcwithoutsource-ing it and complaining "nothing changed"