Linux and Git commands · Lesson 1 of 23
The shell: what happens when you type a command
How the shell finds and runs a program, what PATH does, and how to read a manual or --help before you guess.
On this page
When you connect to a server over SSH, you get a shell: a program that reads a line, runs it, and prints the result. This lesson explains what the shell does between you pressing Enter and output appearing, and how to find answers on the machine itself when you are stuck.
Why this matters#
Almost every server you will touch has no mouse and no browser, only a shell. When a deploy fails with command not found, or a script works on your laptop but not on the server, the cause is usually something in this lesson: how the shell finds programs. Knowing the model saves hours of guessing.
The shell is a program, not the terminal#
Three words get mixed up. The terminal is the window that shows text. The shell is the program inside it that interprets what you type; on most servers it is bash. The command is the first word of the line, and everything after it is arguments.
ls -l /etc/hostnameHere ls is the command, -l is an option, and /etc/hostname is an argument. The shell splits the line on spaces and hands the pieces to the program.
What happens when you press Enter#
The shell goes through a short list of steps. First it splits the line into words. Then it expands things like ~ (your home directory) and * (wildcards, covered in lesson 3). Then it works out what the first word means:
- A builtin: code inside the shell itself, such as
cdandecho. - An alias or function: a shortcut someone defined, often in
.bashrc. - An external program: a file on disk that the shell must find and run.
You can ask the shell which one it is with type:
type cd
type ls
type -a echocd is a shell builtin
ls is /usr/bin/ls
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echocd has to be a builtin. A separate program could not change the directory of your shell, only its own. echo exists in both forms; the builtin wins because the shell checks it first.
PATH: how the shell finds programs#
For an external program, the shell does not search the whole disk. It looks only in the folders listed in the PATH variable, left to right, and runs the first match.
echo $PATH | tr : '\n'/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/binThat is a typical Ubuntu server value; yours may have a few more folders. tr : '\n' just turns the colons into line breaks so it is easy to read. To see which file would run, use type -a or which -a:
type -a lsls is /usr/bin/ls
ls is /bin/lsBoth paths lead to the same program on modern Ubuntu, because /bin is a link to /usr/bin. If two different files shared a name, the first folder in PATH would win. This is why installing a newer Python in /usr/local/bin can shadow the system one in /usr/bin: /usr/local/bin comes earlier in PATH.
Why ./myscript works but myscript does not#
Suppose you write a small script in your current folder and make it executable:
printf '#!/bin/sh\necho hello from myscript\n' > myscript
chmod +x myscript
myscriptbash: myscript: command not foundThe current folder is not in PATH, so the shell never looks there. You give the path explicitly instead. A word containing a slash is treated as a path and PATH is skipped entirely:
./myscripthello from myscriptThe . means the current folder, so ./myscript says "the file called myscript right here". The shell leaves the current folder out of PATH on purpose. If it were included, a file named ls dropped into a shared folder could run instead of the real ls.
To make your own tools available everywhere, put them in a folder that is in PATH, such as /usr/local/bin or ~/.local/bin.
Exit codes: the answer behind the output#
Every program finishes with a number called the exit code. Zero means success and anything else means some kind of failure. The shell stores the last one in $?.
nosuchcmd
echo "exit=$?"
ls /nonexistent
echo "exit=$?"bash: nosuchcmd: command not found
exit=127
ls: cannot access '/nonexistent': No such file or directory
exit=2Exit code 127 is the shell's own way of saying "I could not find that command". Other codes come from the program; ls uses 2 for a missing path. Scripts and deploy tools rely on these numbers, and lesson 12 uses them to stop a script at the first failure.
Getting help on the machine#
You will not remember every option, and you do not need to. Linux ships its own documentation.
--helpprints a short usage summary. It works for most programs and is the fastest check.man <command>opens the full manual page. Pressqto quit,/followed by a word to search, andnfor the next match.type <command>andwhich <command>tell you what will actually run.
ls --help | head -4Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
In a manual page or usage line, square brackets mean an optional part and ... means "can repeat". So ls [OPTION]... [FILE]... says you can give any number of options and any number of files.
Note: the container used to test this lesson has its manual pages removed to save space, so man ls printed a notice instead of a page. A normal Ubuntu server has them installed. If man says there is no entry on a minimal server, run sudo apt install man-db or rely on --help.
Small habits that help#
Press Tab to complete a command or file name; press it twice to list the choices. Press the Up arrow or Ctrl+R to bring back and search old commands. Press Ctrl+C to stop a running program and Ctrl+D on an empty line to log out. These save more typing than any alias.
Try it#
- Run
type -a echoandtype -a cd. Explain why one has a file path and the other does not. - Run
echo $PATH | tr : '\n'. Pick one folder and runlson it to see a few programs the shell can find. - Create the
myscriptfile from this lesson in your home folder. Run it asmyscript, then as./myscript, and note the difference. Then run it by its full path, such as/home/you/myscript. - Run
ls /does-not-existandecho $?. Then runls /andecho $?and compare the two numbers. - Pick a command you use often and read the first screen of its
--helpoutput. Find one option you did not know.
Sign in to keep track of the lessons you finish.