>_How the Terminal Works · Lesson 2 of 7

What Happens When You Press Enter

'ls -l' — Enter — output appears. Between those two moments: parsing, an environment-variable treasure hunt, a process being born, and three data streams being wired up. Here's the whole journey.

Bash
# The shell splits your line into words:
ls -l /tmp
# word 0: 'ls'      -> the command
# word 1: '-l'      -> argument (programs choose what it means)
# word 2: '/tmp'    -> argument

# Then finds the program by searching PATH, in order:
echo $PATH        # /usr/local/bin:/usr/bin:/bin:...
which ls          # /usr/bin/ls — first match wins

# Not everything is a program on disk:
type ls           # ls is /usr/bin/ls
type cd           # cd is a shell builtin  (must be! it changes
                  # the shell's own directory)
type ll           # ll is an alias for 'ls -alF' (maybe)

Before the search, the shell expands your line: $HOME becomes /home/you, *.txt becomes every matching filename, ~ becomes your home directory. The program never sees the wildcard — it receives the already-expanded list. Then the shell asks the OS to fork a child process, exec the program in it with those arguments, and waits for it to exit.

Bash
# Expansions happen BEFORE the program runs:
echo *.md          # shell replaced *.md with actual filenames
echo "$HOME"       # variable expanded by the shell
echo '$HOME'       # single quotes suppress expansion: $HOME

# Every process exits with a status code:
ls /nonexistent
echo $?            # 2 — nonzero means failure
ls /tmp > /dev/null; echo $?    # 0 — success

# && and || read the exit code:
make && ./run      # run only if make succeeded
cmd || echo "failed"
✦ Tip
'Command not found' now debuggable: the shell searched every PATH directory and none contained that name. Installed something and it's not found? Its directory isn't in PATH, or the shell cached an old lookup (hash -r / rehash clears it).