Computing and the Command Line

Variables, PATH, and Aliases

Shell variables versus environment variables, and how export passes a value to the programs you start. The common environment variables (HOME, USER, PATH, EDITOR, LANG). How the shell finds a command: aliases, functions, builtins, then PATH from left to right; why your own scripts need ./ and how to put a personal bin folder on PATH. Aliases for shortcuts and functions for anything that takes arguments.

  • 7 min
  • 8 steps
  • 3 questions
  • Lesson 14 of 80

In this lesson

  1. Shell variables and environment variables
  2. Variables you’ll see everywhere
  3. How the shell finds a command
  4. Your own commands on PATH
  5. Aliases
  6. Functions
  7. Your turn
  8. So

Shell variables and environment variables

You create a variable by writing a name, =, and a value, with no spaces around the = 1 2:

me@linuxbox:~$ project=garden-plan
me@linuxbox:~$ echo "$project"
garden-plan
me@linuxbox:~$ project = garden-plan
project: command not found

With spaces, bash reads project as a command name. Use quotes when the value has spaces: title="Spring garden".

There are two kinds of variables 1:

  • A shell variable exists only in the current shell.
  • An environment variable is also copied to every program the shell starts.

export turns one into the other 1:

me@linuxbox:~$ color=blue
me@linuxbox:~$ bash -c 'echo "color is $color"'
color is
me@linuxbox:~$ export color
me@linuxbox:~$ bash -c 'echo "color is $color"'
color is blue

Programs get their settings this way: the editor to open, your language, where your home is. To set a variable for one command only, put it in front: LANG=C sort names.txt.

printenv lists the environment; set lists everything, shell variables and functions included 1. unset name removes a variable.

Quick check

You set color=blue and then run bash -c 'echo $color'. Why does it print nothing?

Variables you’ll see everywhere

Variable Holds
HOME Your home directory
USER Your user name
PWD, OLDPWD The current and previous directory (cd - uses OLDPWD)
PATH Where to look for commands
SHELL Your login shell
EDITOR The text editor programs should open for you
PAGER The pager, usually less
LANG Your language and character set, e.g. en_US.UTF-8
PS1 Your prompt (next lesson)

These are from the list in Shotts’ book 1. Not every system sets all of them.

How the shell finds a command

When you type a command name, bash checks, in order 1 2:

  1. Is it an alias?
  2. Is it a shell function?
  3. Is it a builtin, like cd or echo?
  4. Otherwise, search the directories in PATH, left to right, and run the first matching file that’s executable 3.
me@linuxbox:~$ echo "$PATH"
/home/me/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
me@linuxbox:~$ echo "$PATH" | tr ':' '\n'      # one per line, easier to read

The first match wins. If two folders both contain a python3, the earlier one in PATH is the one you get. type -a name shows every match in the order they’d be tried, and which name shows the first program file 1.

You type backup. The shell first checks whether it's an alias, a function, or a builtin like cd, echo, or type. If none, it searches PATH, /home/me/.local/bin:/usr/local/bin:/usr/bin:/bin, from left to right: /home/me/.local/bin, your own programs, no backup here; /usr/local/bin, things you installed yourself, no backup here; /usr/bin, the distribution's programs, backup found, run it; /bin, a link to /usr/bin, never reached. If nothing is found: bash: backup: command not found. Below, a slash skips the search: ./backup runs the file in this directory; ~/scripts/backup runs that exact file; type -a backup shows every match in the order tried; export PATH="$HOME/.local/bin:$PATH" puts a folder first.
First match wins, and a slash in the name skips the search. Credit: StudyCorner diagram · CC BY 4.0 · Source

Quick check

You saved a script as ~/hello.sh and made it executable. Why does typing hello.sh say command not found?

Your own commands on PATH

A name with a slash in it skips the search: ./hello.sh means “the file hello.sh in this folder,” and /usr/bin/ls means that exact file 3. That’s why a script in your current folder must be run as ./hello.sh. The current folder is deliberately not on PATH, so a file named ls lying in some folder can’t hijack your ls.

To make your own scripts runnable from anywhere, keep them in a folder that is on PATH. Ubuntu already puts ~/.local/bin and ~/bin on it, if they exist when you log in 1:

me@linuxbox:~$ mkdir -p ~/.local/bin
me@linuxbox:~$ mv ~/hello.sh ~/.local/bin/hello
me@linuxbox:~$ hello                      # works from anywhere (after a new login)

On other systems, or for another folder, add it yourself in your startup file (next lesson) 1:

export PATH="$HOME/.local/bin:$PATH"     # first, so your versions win

Put new folders before $PATH to take priority, or after ("$PATH:$HOME/tools") to be a fallback. Never replace it outright: PATH=~/tools on its own would hide every system command until you open a new shell.

Aliases

An alias is a shortcut for the start of a command 1:

me@linuxbox:~$ alias ll='ls -alF'
me@linuxbox:~$ alias update='sudo apt update && sudo apt upgrade'
me@linuxbox:~$ alias rm='rm -I'
me@linuxbox:~$ alias                  # list them
me@linuxbox:~$ unalias update

Ubuntu’s default .bashrc already defines ll, la, and l, and makes ls and grep colorize. To run the real command and skip an alias, put a backslash in front: \ls.

Aliases typed at the prompt vanish when the shell closes; next lesson puts them in ~/.bashrc so they last.

Functions

An alias can only paste text at the front of a command. When you need arguments in the middle, or several steps, write a function 1 2:

mkcd() {
    mkdir -p "$1" && cd "$1"
}
me@linuxbox:~$ mkcd projects/shed-plans
me@linuxbox:~/projects/shed-plans$

$1 is the function’s first argument, $2 the second, and "$@" all of them. A function runs inside your current shell, which is why cd in a function moves you, while cd in a script would only move the script. You’ll write many more in the scripting module.

Quick check

You want a shortcut mkcd photos that makes a folder and moves into it. Alias or function?

Your turn

Exercises

  1. Set a variable garden="raised beds" and print it in a sentence. Then show that bash -c 'echo $garden' doesn’t see it until you export it.
  2. Print your PATH one folder per line. Which folder comes first?
  3. Run type -a echo ls cd. Explain each answer.
  4. Make ~/.local/bin, put a two-line script there (#!/bin/bash and echo "hi from my bin"), make it executable, and run it by name. (If it’s “not found,” log out and back in, or close and reopen WSL.)
  5. Write a function bak that copies a file to the same name plus .bak. Test it on a scratch file.
Answers
  1. echo "My garden has $garden". After export garden, bash -c 'echo $garden' prints raised beds.
  2. echo "$PATH" | tr ':' '\n'. On Ubuntu, usually ~/.local/bin once it exists, otherwise /usr/local/sbin.
  3. echo is a shell builtin and also a program /usr/bin/echo (the builtin wins); ls is an alias and also /usr/bin/ls; cd is only a builtin.
  4. mkdir -p ~/.local/bin, create ~/.local/bin/sayhi with those two lines, chmod +x ~/.local/bin/sayhi, then sayhi. Ubuntu’s ~/.profile adds the folder to PATH only if it existed at login, which is why a fresh login may be needed the first time.
  5. bak() { cp -- "$1" "$1.bak"; }. The -- tells cp that nothing after it is an option, which protects you from a file name starting with -.

So

Variables are name=value with no spaces; export makes them environment variables that programs inherit. The shell looks up a command as an alias, then a function, then a builtin, then in PATH, left to right, first match wins; a name with a slash skips the search. Keep your own scripts in ~/.local/bin, use aliases for shortcuts, and use functions when you need arguments.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 6 min

Startup Files and the Prompt

Next lesson
Sources for this lesson
  1. 1
    William Shotts. The Linux Command Line, Seventh Internet Edition (25.12A). LinuxCommand.org (print edition by No Starch Press). 2026. verifiedFree CC BY-NC-ND 3.0 book, release 25.12A of July 18, 2026. Part 1, Learning the Shell: the shell and terminal emulators, prompts ($ vs. # for the superuser), command history (most distributions keep the last 1,000 commands), Shift-Ctrl-C/V for copy and paste; navigation and the directory tree; exploring the system (ls options and the long listing, file, less, the guided tour of /, symbolic links); manipulating files (wildcards and character classes, mkdir, cp, mv, rm, ln; no undelete, test wildcards with ls first); working with commands (four kinds of commands, type, which, help, --help, man and its sections, apropos, whatis, info, alias); redirection; expansion and quoting; Readline keyboard tricks, completion, history search; permissions; processes. Later parts cover the environment, vi, packages, storage, networking, find, archiving, regular expressions, text processing, and shell scripting.
  2. 2
    Chet Ramey, Brian Fox. Bash Reference Manual, Edition 5.3. GNU Project, Free Software Foundation. 2025. verifiedThe reference for Bash 5.3 (May 18, 2025). Redirections (3.6) are processed left to right and order matters: ls > dirlist 2>&1 sends both streams to dirlist, while ls 2>&1 > dirlist sends only standard output there. With set -o noclobber, > fails on an existing regular file and >| overrides it. &> word is equivalent to > word 2>&1 and &>> word to >> word 2>&1. Here documents (<<word, with <<- stripping leading tabs; quoting word disables expansion) and here strings (<<<). Expansions (3.5) happen in a fixed order: brace; tilde, parameter, arithmetic, and command substitution left to right; word splitting; filename expansion; quote removal last. Startup files (6.2): an interactive login shell reads /etc/profile then the first of ~/.bash_profile, ~/.bash_login, ~/.profile; an interactive non-login shell reads ~/.bashrc. HISTCONTROL (ignorespace, ignoredups, ignoreboth), HISTSIZE, HISTFILESIZE; set -x traces expanded commands; shell functions and variables, export.
  3. 3
    Anish Athalye, Jon Gjengset, Jose Javier Gonzalez Ortiz. Course Overview + Introduction to the Shell (The Missing Semester of Your CS Education, 2026). MIT CSAIL. 2026. verifiedCC BY-NC-SA lecture notes. The shell is a textual interface for running programs and wiring them together; a terminal is the visual interface to it. Bash is the most widely used shell (zsh and fish are popular alternatives); on Windows use WSL or a Linux VM. The shell splits a command at whitespace: first word the program, the rest arguments; quote or backslash-escape spaces. man and --help, plus tldr for examples; cd is a shell builtin; Tab completion; pwd and $PWD; absolute vs. relative paths, . and ..; $PATH lists the directories searched for programs, which shows the one found. Basic tools cat, sort, uniq, head, tail, grep.