Lesson 2 of 7 · 0 of 1 exercises solved
What Happens When You Press Enter
Follow a command from the moment you press Enter: the splits and expands it, finds the program using , starts a and reads its .
# 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 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 . Then the shell asks the OS to fork a child process, exec the program in it with those , and waits for it to exit.
# 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"Practice
Shell exercise
3 checksPATH and exit codes
Explore how the shell finds and reports on commands. Use which to locate ls, print your PATH, run a command that does not exist, then print its exit status with $? on the very next line.
Practice shell — type help to see what is available.
Finished reading?1 exercise still open. You can come back to it later.
Tip: use the ← and → keys to move between lessons.