Environment Variables Basics - Configuration and Practical Usage

Environment Variables Basics - Configuration and Practical Usage

What You'll Learn

  • Check environment variable values with echo $VAR or env
  • Define your own environment variables with export
  • Understand PATH and 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?

Lina: Linny-senpai, I keep seeing things like $HOME and $PATH in tutorials. What are those? The $ sign feels like a magic spell I don't understand.
Linny-senpai: Great question! Those are called "environment variables." Think of them as named notes your shell remembers.
Lina: Named notes?
Linny-senpai: Yes. For example, the shell remembers "Lina's home directory is /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 $VAR or env | 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

Linny-senpai: Let's start by reading, not setting. Looking first is always safer than changing.

1-1. Show one variable: echo $VAR

$ echo $HOME
/home/lina
$ echo $USER
lina

Key Points:

  • $ means "expand this variable's value"
  • Without $, echo HOME just prints the literal text "HOME"
  • Variable names are uppercase by convention (not required, but it's the norm)
Lina: Wow, the $ makes a huge difference!
Linny-senpai: It's the most common beginner stumble. Remember: "the $ 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
Lina: There are so many... do I need to memorize all of them?
Linny-senpai: Nope. In practice, only HOME and PATH really matter. The rest is just "good to know it exists."

2. Setting Variables: Assignment vs export

Linny-senpai: Now we'll set our own variables. There's a classic trap here, so let's go slowly.

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
Lina: What's the actual difference between assignment and export? They look the same to me.
Linny-senpai: Important question! A plain assignment is only visible inside the current shell. Child processes (scripts and commands you launch) don't see it. Add 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

Lina: I keep getting "command not found" errors. Is that related to environment variables too?
Linny-senpai: Bullseye. Understanding 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
Lina: So ls actually lives in /usr/bin/!
Linny-senpai: Exactly. The reason you can just type 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

Lina: I set a variable with export, but when I closed the terminal it disappeared...
Linny-senpai: That's the "persistence" topic. 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.

Lina: What does source actually do?
Linny-senpai: It tells the shell "re-read this file inside the current shell." Normally, running a script spawns a child process, so any export inside it dies with the child. source runs the lines in your current shell, so the changes stick.

5. Common Pitfalls

Linny-senpai: You've got the basics. Let me close with the mistakes 99% of beginners hit.

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

Linny-senpai: Knowledge sticks faster when you type it. Let's do three quick exercises.

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!
Lina: It worked! Running my own command from anywhere feels surprisingly cool.
Linny-senpai: That's the power of PATH. "Command not found" won't scare you anymore.

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 $HOME in single quotes and wondering why it isn't expanded
  • Editing .bashrc without source-ing it and complaining "nothing changed"

Next Reading