Code of the Day
BeginnerShell basics

Variables and Environment

Store values in shell variables, export them to the environment, understand $PATH, and master quoting rules.

BashBeginner12 min read
Recommended first
By the end of this lesson you will be able to:
  • Assign and read shell variables with VAR=value and $VAR
  • Explain the difference between shell variables and environment variables
  • Use export to make variables available to child processes
  • Describe what $PATH does and how it controls which programs you can run
  • Apply correct quoting rules — single quotes vs double quotes

So far, every command you've run is stateless — once it finishes, nothing is remembered. Variables change that. They let you store values, pass information between commands, and build scripts that adapt to their environment. They're also the mechanism that ties the shell to the rest of your system.

Shell variables

Assign a variable with NAME=value. No spaces around =:

name="Alice"
greeting="Hello"
count=42

Read the value by prefixing the name with $:

echo $name           # Alice
echo $greeting       # Hello
echo "$greeting, $name!"   # Hello, Alice!

No spaces around =. name = "Alice" is a syntax error — the shell tries to run a program called name with arguments = and "Alice". This is one of the most common beginner mistakes. Remember: assignment has no spaces.

Quoting rules

The shell expands $VAR inside double quotes but not inside single quotes:

name="Alice"
echo "Hello, $name"    # Hello, Alice   — variable expanded
echo 'Hello, $name'    # Hello, $name   — literal, no expansion

Use double quotes when you want variable values included. Use single quotes when you want the exact string preserved (useful for regular expressions, SQL queries, or strings that contain dollar signs).

There's a third form: command substitution with $(). It runs a command and substitutes its output:

today=$(date +%Y-%m-%d)
echo "Today is $today"
files=$(ls | wc -l)
echo "There are $files files here"

This is how scripts capture the output of one command and use it as input to another.

Environment variables

Shell variables are local to the current shell session. are inherited by child processes — any program you run from the shell gets a copy of them.

Make a variable into an environment variable with export:

export API_URL="https://api.example.com"
export DEBUG=true

To see all environment variables currently set:

env

The output is long. Pipe it to grep to filter:

env | grep PATH
env | grep HOME

When the agent's away: AI coding agents frequently read environment variables for API keys, database URLs, and feature flags. When you set export OPENAI_API_KEY="..." in your shell and then run an agent, the agent inherits that variable. This is the standard way to provide secrets without hardcoding them in files.

$PATH — how the shell finds programs

$PATH is the most important environment variable. It's a colon-separated list of directories the shell searches when you type a command name:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

When you type ls, the shell looks for an executable called ls in each directory in $PATH from left to right. The first match wins. If nothing is found, you get "command not found."

You can add a directory to $PATH:

export PATH="$HOME/bin:$PATH"   # prepend ~/bin so your scripts take priority

This is why installing a tool sometimes requires adding a line like this to your shell configuration file (~/.bashrc or ~/.bash_profile).

Check your understanding

  1. 1.
    Which of the following correctly assigns the value "world" to the variable greeting?
  2. 2.
    Given name="Bob", what does echo 'Hello, $name' print?
  3. 3.
    A shell variable created without export is automatically visible to child processes like scripts you run.

Do it yourself

Try these in your terminal:

greeting="Hello"
name=$(whoami)
echo "$greeting, $name! You are in $(pwd)."

# See quoting difference
echo "My name is $name"
echo 'My name is $name'

# Inspect environment
env | grep HOME
echo $PATH

Then check what's in your $PATH and see if you can find where ls lives:

which ls        # prints the full path of the ls program
which python3   # or python — see what's installed

Where to go next

You can now store values, expand variables, and understand how the environment connects the shell to the programs running in it. Next: scripts and permissions — writing your first .sh file, the shebang line, chmod +x, and positional arguments.

Finished reading? Mark it complete to track your progress.

On this page