T6 Linux and Bash · Lesson 1
The terminal
Talking to a computer in words: what a terminal, a shell and a prompt are, the shape of every command, options and arguments, finding help from the commands themselves, reading error messages and exit statuses, the keys that save typing, and how Linux, its kernel and its distributions fit together.
- say what a terminal, a shell and a prompt are, and run your first commands
- take any command apart into its name, options and arguments
- find out what a command can do, from --help, man and type
- read an error message, and use the exit status with ; && and ||
- save typing with Tab, the arrow keys and Ctrl+C
- say what Linux, the kernel, a distribution and the shell each are
The back cover
Behind every app and every web server sits a plainer way of working with a computer: typing words and reading answers. Programmers use it every day, because a few typed words can do what a hundred clicks cannot, and can be saved and run again.
These pages show the terminal, the shell and the prompt, and the shape that every command shares. They show how to get help from the commands themselves, how to read errors and exit statuses, and the keys that save typing. Last comes Linux itself: the kernel, GNU and the distributions built from them.
Before you start
The story: The speaking-tube
Master Verge's iron key opens a low door behind the Clock Tower, and forty steps down you reach the Underway: the tunnels beneath Brasshaven, where the wagons, pumps and doors all obey spoken commands. There is a brass speaking-tube in the wall by the first gate. You lean in and say, politely, "Open the gate, please."
The tube answers in one flat line: open: command not found. The gate stays shut.
Warden Anwen Rao does not look up from her lamp. "The Underway has no manners," she says. "It has words. Say what you mean. The tunnels will do exactly that."
So far you have told the browser what to do through a page. Many of the computers behind websites have no screen and no mouse. They are servers, and people work on them by typing short commands. Git, Node.js and Docker, which come later in this school, are driven by typed commands too. This step teaches that way of working, starting with the window you type in.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Files and folders. | A file named index.html in a folder. | Commands act on them. |
| Paths in links. | href="images/logo.png" | The same idea, in the next lesson. |
| The Console. | Type a line, and read the answer. | A terminal works the same way. |
| Reading an error. | ReferenceError: x is not defined | Shell errors also name what went wrong. |
| Arguments to a function. | greet("Asha") | Commands take arguments too. |
| Options objects. | { seats: 2 } | Options change how a command works. |
| Text output. | console.log(...) | Most commands print text. |
| A status code. | 200, 404 | Commands report success as a number. |
| true and false. | a && b | The shell's && and || work on success. |
| Running code with Node. | node app.js | That line was a shell command. |
By the end of this lesson you will be able to:
- say what a terminal, a shell and a prompt are, and run your first commands;
- take any command apart into its name, options and arguments;
- find out what a command can do, from --help, man and type;
- read an error message, and use the exit status with ; && and ||;
- save typing with Tab, the arrow keys and Ctrl+C;
- say what Linux, the kernel, a distribution and the shell each are.
How to follow this lesson
Every block with a Run button works on this page. It runs in a small shell written for this school, on a folder that lives only in your browser, so nothing you do can harm anything. Each block was also run in real bash on a real Linux system, and both had to print exactly what you see. Blocks with a prompt and no Run button show a real terminal, for what the small shell cannot do.
In one line
A terminal is a window for typing commands; this lesson teaches how to speak to it, and how to read what it says back.
The terminal, the shell and the prompt
At a glance
A shop counter has a window, and behind it a clerk who understands what you ask. The terminal is the window; the shell is the clerk.
The terminal is the window you type in, and the shell, usually bash, is the program that understands what you type. When it is ready, it shows a prompt. You type a command, press Enter, and read the answer. No answer at all usually means the command worked, because quiet success is the habit of these tools.
Take a guess: Why does the prompt of some users end in # instead of $?
Three words get mixed up, so take them one at a time. Think of a telephone at a shop. The telephone is what you hold; the person who answers is who actually does the work.
The window you type in. It shows text and sends your keys on; by itself it understands nothing. Linux and the Mac each come with an app called Terminal. On Windows, a feature called WSL, the Windows Subsystem for Linux, gives you a Linux terminal.
The program that reads what you type, works out what you mean, runs it, and prints the answer. The usual shell on Linux is bash, the Bourne Again Shell, and this step teaches bash. A Mac's Terminal opens a different shell, zsh, by default.
So the terminal is the telephone, and the shell is who picks up. When a terminal opens, the shell prints a prompt and waits. A prompt usually says who you are, which computer you are on and where you are:
Terminal
asha@tower:~/shop$ whoami
asha
asha@tower:~/shop$ pwd
/home/asha/shop
Read the prompt from the left. asha is the user. After the @ comes tower, the computer's name. After the colon, ~/shop is the folder you are in, and ~ means your home folder. The $ means the shell is ready for you. You type what comes after it and press Enter; the answer appears below, and a new prompt follows.
The text a shell prints when it is ready for a command. It is not part of the command: never type the $ yourself. A prompt that ends in # means you are the all-powerful root user.
Your first commands
The blocks in this lesson show only what you would type after the prompt. pwd, short for print working directory, says which folder you are in. whoami says who you are. echo prints what you give it:
pwd
whoami
echo Namaste
/home/asha/shop
asha
Namaste
Press Run, then Edit, change the word after echo, and run it again. Commands run one after another, top to bottom, and each prints its answer. ls, short for list, shows what is in the folder. This block starts in a folder that holds two folders and a file:
In a folder with pumps, logs and notes.txt
ls
logs notes.txt pumps
Quiet commands are normal. Many commands print nothing when all goes well, and the next prompt appears. In the shell, no news is good news.
| Word | Means | Telephone |
|---|---|---|
| terminal | the window | the telephone |
| shell (bash) | the program that understands commands | the person who answers |
| prompt | the shell saying it is ready | "Hello?" |
| command | what you ask for | your request |
Worked example 1
Warden Rao asks where you stand in the Underway, and under what name the tunnels know you. Find both, and greet the tube with your own message, keeping its double space.
What we have: a terminal, and two questions
What we need: the folder, the user, and a message printed exactly
The plan: pwd gives the folder, whoami the user, and echo prints the message; quotes keep its spaces
pwd
whoami
echo "Reporting from below"
/home/asha/shop
asha
Reporting from below
- Without the quotes the shell would split the words, and echo would print them with one space. The next section explains why.
You are at /home/asha/shop, known as asha.
Check: the folder printed matches the ~/shop of the prompt, as ~ is /home/asha.
A path that fails: typing the prompt as well: $ pwd. The shell takes the $ as the name of a command, and says it cannot find one. Copy only what comes after the prompt.
When you are stuck: nothing happens, or the terminal seems to hang
Press Enter: a command runs only after Enter.
If there is no prompt, a command is still running: press Ctrl+C to stop it.
A > at the start of the line means the shell thinks your line is unfinished, often because of a missing quote: press Ctrl+C and type it again.
No error and no output usually means that the command worked.
Your turn
echo Hello
pwd
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Hello
/home/asha/shop
- echo prints its words.
- pwd prints the folder that a block starts in.
It prints Hello, then /home/asha/shop.
In the prompt asha@tower:~/shop$, what is tower?Tap to reveal
- The name after the @ is the computer.
The computer's name.
What is the difference between the terminal and the shell?Tap to reveal
- One shows text; the other understands commands.
The terminal is the window; the shell is the program that runs your commands.
Did you know?
A shell named with a pun
The shell you type into has a pun for a name. GNU calls bash the Bourne Again SHell: an sh-compatible shell, following the older Bourne shell, with useful features from the Korn shell and the C shell. Its original author was Brian Fox, and it was first released on 8 June 1989. Its developer today is Chet Ramey.
Why it fits here: It names this topic's usual shell: bash, born in 1989 as a free shell that runs the commands of the older sh.
Sources: GNU Bash; Wikipedia: Bash (Unix shell)
$ for you, # for root
The last character of the prompt says who you are. The bash manual explains the prompt code \$: it shows # if the user's ID is 0, and $ otherwise. On Unix-like systems, the user with ID 0 is the superuser, whatever the account is called, so a prompt ending in # warns that every command can change the whole system.
Why it fits here: It answers this topic's question: a prompt ending in # belongs to the superuser, the account that may change anything.
Sources: GNU Bash Reference Manual: Controlling the Prompt; Wikipedia: Superuser
In one line
The terminal is the window, and the shell, usually bash, is the program that understands what you type. It shows a prompt when it is ready. You type a command, press Enter, and read the answer; no answer usually means success.
The shape of a command
At a glance
A good instruction names the job, says how to do it, and says what to do it to: wash, gently, the shirts. A command has the same three parts.
A command is a name, then options that change how it works, then arguments it works on, all separated by spaces. Short options use one dash and can be joined, as in -la; long options use two dashes, as in --all. So ls -l -a and ls -la mean the same. Capitals count, and quotes keep words with spaces together.
Take a guess: Who decided that options start with a dash, and who added the long ones with two?
Nearly every command has the same three parts, and once you can see them, every new command is half learned.
In a folder with notes.txt
ls -l notes.txt
-rw-rw-r-- 1 asha asha 0 Jan 1 10:00 notes.txt
| Part | Here | What it does |
|---|---|---|
| the command | ls | the job to do |
| an option | -l | how to do it: here, in the long form |
| an argument | notes.txt | what to do it to |
A word that starts with a dash and changes how a command works. Short options are one letter, -l; long options are whole words, with two dashes, --all. Many options have both forms. Options are also called flags.
A word that gives a command something to work on, such as a file name. The word is sometimes used for options too.
Spaces separate the parts, which makes them matter. ls-l is one word, and no command has that name.
Options can be joined
Short options can be written apart or together, in any order. All of these mean the same. Option -a shows everything, hidden files too: a file whose name starts with a dot is hidden from a plain ls.
In a folder with pumps, logs, notes.txt and a hidden .secret
ls -l -a | wc -l
ls -la | wc -l
ls -al | wc -l
ls -l --all | wc -l
7
7
7
7
The | wc -l on each line counts the lines of the answer, so that the block can show they agree; the pipes lesson explains it. Here is the plain answer, with the hidden file and two strange entries, . and .., which the next lesson explains:
In a folder with pumps, logs, notes.txt and a hidden .secret
ls -a
. .. .secret logs notes.txt pumps
Capitals and spaces count
Linux treats capital and small letters as different. ls is a command, and LS is not:
LS
bash: line 1: LS: command not found
Spaces between words are only separators, so the shell drops extra ones. Quotes keep text together as one argument, spaces and all:
echo Namaste Brasshaven
echo "Namaste Brasshaven"
Namaste Brasshaven
Namaste Brasshaven
In the first line, echo received two arguments, and printed them with one space. In the second, it received one. The same rule applies to a file whose name has a space in it, which needs quotes every time.
Comments and long lines
A # starts a comment: the shell ignores the rest of the line. A backslash at the very end of a line continues the command on the next line:
# this whole line is ignored
echo hello # and so is this part
echo one \
two
hello
one two
Worked example 2
Show everything in the folder, hidden files included, in the long form with file sizes easy to read. The option -h means human-readable sizes. Then show it again for only notes.txt.
What we have: the options -l, -a and -h, and one file name
What we need: the two commands, with each part in its place
The plan: join the options after ls; add the file as an argument for the second
In a folder with notes.txt and a hidden .secret
ls -lah | wc -l
ls -lh notes.txt
5
-rw-rw-r-- 1 asha asha 0 Jan 1 10:00 notes.txt
- The first line counts five lines: a total, then ., .., .secret and notes.txt.
ls -lah shows everything; ls -lh notes.txt shows one file.
Check: the options stayed together after a single dash, and the argument came last.
A path that fails: writing ls -lah-notes.txt. With no space, the shell sees one word, and ls reads every letter after the dash as an option.
When you are stuck: a command does not do what you expected
Check the spaces: one between the command, each option and each argument.
Check capitals: ls and LS are different, and so are Notes.txt and notes.txt.
A name with a space needs quotes: "my notes.txt".
Short options take one dash, and long options two.
Your turn
echo a b
echo "a b"
echo a b # c
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
a b
a b
a b
- Extra spaces between arguments are dropped.
- Quotes keep them.
- The # starts a comment, so c never reaches echo.
It prints a b, then a b, then a b.
Name the parts of head -n 5 log.txt.Tap to reveal
- The first word is the command.
- -n is an option, and 5 is its value.
- log.txt is what it works on.
Command head, option -n 5, argument log.txt.
Which is the same as ls -l -a -h?Tap to reveal
- Short options can be joined after one dash, in any order.
ls -hal, for one.
Did you know?
One dash, by the standard
The rule that options begin with a dash is written in a standard. POSIX, the standard that Unix-like systems share, sets out guidelines for how utilities take their options, and its Guideline 4 says that all options should be preceded by the - delimiter character. That shared rule is why ls -l, grep -i and sort -r all look alike.
Why it fits here: It answers this topic's question: the single dash before an option comes from the POSIX standard.
Source: The Open Group Base Specifications: Utility Conventions
Two dashes from GNU
Long options with two dashes were GNU's addition. The GNU Coding Standards ask programmers to define long-named options that are equivalent to the single-letter Unix-style options, and give the reason in one line: we hope to make GNU more user friendly this way. So --all can be read as a word, where -a must be remembered.
Why it fits here: It explains this topic's two kinds of option: short ones from Unix, and long readable ones added by GNU.
Source: GNU Coding Standards
In one line
A command is a name, then options that change how it works, then arguments it works on, separated by spaces. Short options use one dash and can be joined; long ones use two. Capitals count, and quotes keep words together.
Getting help from the commands themselves
At a glance
A new appliance comes with a quick-start card and a thick manual. Commands carry both, built in. Reading them first saves guessing.
Ask the command itself: --help gives the short guide, man gives the full manual, and q leaves it. The help command explains a builtin, and type tells you which kind a command is. In a usage pattern, square brackets mean optional, CAPITALS are placeholders, and three dots mean the part can repeat. The manual is always there, even with no internet.
Take a guess: Why might man printf show a page about the C language instead of the command?
No one remembers every option of every command, and you do not need to. The commands carry their own instructions. There are three places to look, and this section shows which to use when.
--help: the short guide
Most programs accept the long option --help. They print a short guide and stop, without doing their usual work. Here it is for wc, short for word count. A help text can be long, so | head -12 keeps only its first twelve lines:
Terminal
asha@tower:~/shop$ wc --help | head -12
Usage: wc [OPTION]... [FILE]...
or: wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. A word is a nonempty sequence of non white
space delimited by white space characters or by start or end of input.
With no FILE, or when FILE is -, read standard input.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
-c, --bytes print the byte counts
-m, --chars print the character counts
The small shell on this page has no help texts, so this block shows a real terminal, and the Run button is missing on purpose. Run it on any Linux machine and you will see the same words.
Read the Usage line as a pattern, not as something to type. Its marks have fixed meanings, and they are the same in nearly every help text:
| In the pattern | It means | So for wc |
|---|---|---|
wc | type this exactly | the command's name |
[OPTION] | square brackets: you may leave it out | options are not needed |
| words in CAPITALS | a placeholder: put a real thing here | FILE becomes notes.txt |
... | you may repeat it | several options, several files |
So wc, wc notes.txt and wc -l notes.txt logs.txt all fit the pattern. Further down, the help text lists each option, short form first:
Terminal
asha@tower:~/shop$ wc --help | grep lines
-l, --lines print the newline counts
Here grep lines keeps only the lines of the help text that contain the word lines, which is the fastest way to search a long help text. The pipes lesson teaches it properly. The answer says that -l and --lines are two names for one option.
man: the full manual
Most programs also have a manual page, which is longer and more careful. You open one with man wc, and it fills the window until you leave it. Its headings are nearly always the same: NAME, SYNOPSIS, DESCRIPTION, and often OPTIONS and EXIT STATUS. The SYNOPSIS is the same kind of pattern as the Usage line.
A manual page opens in a program for reading long text, usually one called less, and that program has its own keys:
| Key | In a manual page |
|---|---|
| Space | forward one window |
| b | back one window |
| the up and down arrows | one line at a time |
| / then a word, then Enter | search forward for the word |
| n | the next match |
| q | quit, and return to the prompt |
The key to remember is q. Beginners open a manual page, cannot find the way out, and close the whole terminal. There is no man in the small shell, which tells you so:
In a real terminal, this opens the manual page of wc
man wc
type and help: the commands inside the shell
Some commands are programs, kept as files in a folder such as /usr/bin. Others are built into the shell itself, and those are called builtins. type tells you which is which:
type ls
type cd
type nothing
ls is /usr/bin/ls
cd is a shell builtin
bash: line 3: type: nothing: not found
A command that is part of the shell, not a separate program. cd has to be one: it changes the shell's own current folder, and a separate program cannot reach inside the shell to do that.
A builtin usually has no manual page of its own. Bash has a command for those, named help:
Terminal
asha@tower:~/shop$ help cd | head -2
cd: cd [-L|[-P [-e]]] [-@] [dir]
Change the shell working directory.
In that pattern a bar, |, means one or the other. You will rarely need more than the second line, which says what the command does.
| You want | Type | It works on |
|---|---|---|
| a short guide | wc --help | programs |
| the full manual | man wc | programs, and bash itself |
| a builtin's guide | help cd | builtins |
| what kind of command it is | type cd | anything |
| which version you have | wc --version | programs |
The last row is useful when a tutorial and your machine disagree. A version is the first thing to check:
Terminal
asha@tower:~/shop$ wc --version | head -1
wc (GNU coreutils) 9.7
asha@tower:~/shop$ bash --version | head -1
GNU bash, version 5.3.9(1)-release (x86_64-pc-linux-gnu)
Worked example 3
The gate log is in notes.txt. Warden Rao wants the number of lines in it, and nothing else. You know that wc counts things, but not which option you need. Find the option, then use it.
What we have: the program wc, and a file notes.txt of three lines
What we need: the option for lines, then the count
The plan: ask the program itself with --help, search for the word lines, then use what it says
Terminal
asha@tower:~/shop$ wc --help | grep lines
-l, --lines print the newline counts
- The help text says that -l counts newlines, and each line of the file ends in one.
In a folder with notes.txt, three lines long
wc -l notes.txt
wc --lines notes.txt
3 notes.txt
3 notes.txt
- Both spellings work. The answer gives the count, then the file's name.
The file has 3 lines; the option is -l, or --lines.
Check: read the file with cat notes.txt and count: pump one, pump two, valve.
A path that fails: guessing wc -lines notes.txt. One dash means single letters, so wc reads l, then i, and stops there with an error: invalid option -- 'i'.
When you are stuck: you cannot find what a command does
Try name --help first. If it says invalid option, try man name.
If man says there is no manual entry, ask type name: a builtin needs help name.
If help scrolls past too fast, add | head -20 or | less; in less, q leaves.
Stuck inside a manual page? Press q.
Your turn
type pwd
type ls
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
pwd is a shell builtin
ls is /usr/bin/ls
- pwd is built into bash.
- ls is a program in /usr/bin.
It prints pwd is a shell builtin, then ls is /usr/bin/ls.
A help text says head [OPTION]... [FILE].... Must you give head a file name?Tap to reveal
- Square brackets mean you may leave that part out.
- FILE is in square brackets.
No: the brackets say it is optional.
You are reading man wc and want to get back to the prompt. Which key?Tap to reveal
- Manual pages open in a reader with its own keys.
- q stands for quit.
q.
Did you know?
The manual's numbered sections
The manual is split into numbered sections. The Linux man-pages project lists them: 1 for user commands, 2 for system calls, 3 for library calls, and more. A name can live in two sections at once: printf is a command that formats and prints data, in section 1, and a family of C functions, in section 3. So man 1 printf asks for the command.
Why it fits here: It answers this topic's question: man searches numbered sections, and printf has a page in two of them.
Sources: Linux man-pages: man-pages(7); Linux man-pages: printf(1); Linux man-pages: printf(3)
Manuals made short
When a manual page is too long, a shorter one exists. The tldr pages describe themselves as simplified and community-driven man pages: a community effort to simplify the beloved man pages with practical examples. Each page shows a few common uses of a command, so it suits a moment when the full manual is more than you need.
Why it fits here: It adds a friendlier helper to this topic's --help and man: short pages of examples, written by users.
Source: tldr pages
In one line
Ask the command: --help gives the short guide, man gives the full manual (press q to leave), help explains a builtin, and type tells you which kind a command is. In a usage pattern, square brackets are optional, CAPITALS are placeholders, and dots mean repeat.
Reading errors and exit statuses
At a glance
A good complaint names who is complaining, about what, and why. Error messages in the terminal follow the same order.
Read an error from the left: the program that complained, the thing it complained about, and the reason. Every command also leaves an exit status in $?: 0 means success, and any other number means failure. Join commands with ; to go on regardless, && to go on only after success, and || to go on only after failure. So mkdir notes && cd notes enters the folder only if it was made.
Take a guess: What exit status does bash give a command that does not exist?
The Console taught you to read an error from the left: what went wrong, and where. The shell's errors are shorter, and they follow one pattern. The first word says who is complaining, the middle says what it tried, and the end says why it could not.
ls nothing.txt
ls: cannot access 'nothing.txt': No such file or directory
Read it in that order. ls is complaining. It could not reach nothing.txt. There is no such file. So you check the spelling, or the folder you are in. Compare the error from a command that does not exist at all:
Ls
bash: line 1: Ls: command not found
This time the complaint comes from bash, not from a program, because there was no program to run. The words line 1 appear because each block on this page runs as a small script. In a terminal you type in, the same mistake says bash: Ls: command not found.
The errors you will meet most
| The message ends in | It means | Check |
|---|---|---|
command not found | no command has that name | spelling and capitals |
No such file or directory | that name is not here | spelling, and which folder you are in |
Is a directory | it wanted a file and got a folder | the name; rm needs -r for a folder |
Not a directory | it wanted a folder and got a file | the name |
File exists | that name is already taken | another name |
missing operand | it needs an argument | add what it should work on |
invalid option | that option does not exist | one dash or two; --help |
Permission denied | you are not allowed | the permissions lesson |
Most of them name the exact thing. Do not skip past them: the fix is usually in the sentence.
The exit status
A command also leaves a quiet answer that is never shown on the screen. When it ends, it hands the shell a number, called its exit status. Zero means success. Anything else means something went wrong. It works like an HTTP status, only with one good number in place of the 200s.
A whole number from 0 to 255 that a command gives the shell as it ends. 0 means success, and any other number means failure. The shell keeps the last one in the special name $?.
You read it with echo $?, right after the command you care about:
pwd
echo $?
ls nothing.txt
echo $?
Ls
echo $?
/home/asha/shop
0
ls: cannot access 'nothing.txt': No such file or directory
2
bash: line 5: Ls: command not found
127
pwd worked, so 0. ls could not find its file, so 2, which its help text calls serious trouble. A command that cannot be found always gives 127. Each command chooses its own failure numbers, and their manual pages list them. All you need to remember is that 0 is success and anything else is not.
Zero is the good one
In JavaScript, 0 counts as false. In the shell it is the opposite: 0 means success, and the rest mean failure. There is one success and many ways to fail, and the different numbers name the different failures.
$? always holds the status of the very last command, and that includes an echo. So a second look gives you echo's status, not the first one:
false
echo $?
echo $?
1
0
true and false are tiny commands that do nothing except succeed and fail, which makes them handy for practice.
Joining commands: ; and && and ||
You can put several commands on one line. How they are joined says what happens if one fails. A semicolon, ;, means run the next one anyway. Two ampersands, &&, mean run the next one only if this one succeeded. Two bars, ||, mean run the next one only if this one failed.
ls nothing.txt; echo next
ls nothing.txt && echo found
ls nothing.txt || echo missing
ls: cannot access 'nothing.txt': No such file or directory
next
ls: cannot access 'nothing.txt': No such file or directory
ls: cannot access 'nothing.txt': No such file or directory
missing
Each line fails at ls in the same way. In the first, the semicolon runs echo anyway. In the second, && stops: found is never printed. In the third, || runs echo because ls failed.
| Join | Runs the next command | Read it as |
|---|---|---|
a ; b | always | then |
a && b | only if a succeeded | and then |
a || b | only if a failed | or else |
These are the same marks you know from JavaScript, doing the same job. There, the right side is skipped if the left side settles the answer. Here, success plays the part of true, and failure the part of false.
Worked example 4
Warden Rao wants a folder named repairs made, and then the word ready printed, but only if the folder was really made. If anything goes wrong, print see the warden. Write one line, and run it twice, because the second run must fail.
What we have: the command mkdir, which makes a folder, and fails if the name is taken
What we need: one line that reports success or failure
The plan: join with && for what follows success, and || for what follows failure
mkdir repairs && echo ready || echo "see the warden"
mkdir repairs && echo ready || echo "see the warden"
ready
mkdir: cannot create directory ‘repairs’: File exists
see the warden
- The first run makes the folder, so && prints ready. In the second, the name is taken, so mkdir fails, && skips echo ready, and || runs its echo.
The first run prints ready; the second prints mkdir's error, then see the warden.
Check: a folder named repairs exists after the first run, so the second must fail.
A path that fails: joining with semicolons: mkdir repairs; echo ready prints ready even when mkdir failed, which is a report you cannot trust.
When you are stuck: a line of commands does the wrong thing
Run each part alone, and check echo $? after it.
Remember that ; ignores failure, and that && and || look only at the status of the command just before.
Read the first error, not the last. A later error is often caused by the first one.
Read the message from the left: who complained, about what, and why.
Your turn
true && echo one
false && echo two
false || echo three
true || echo four
false; echo five
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
one
three
five
- true succeeds, so && runs echo one.
- false fails, so && skips echo two.
- false fails, so || runs echo three.
- true succeeds, so || skips echo four.
- ; always runs what follows.
It prints one, three, five, each on its own line.
What does echo $? print straight after a command that could not be found?Tap to reveal
- A command that bash cannot find always gives the same status.
127.
Write one line that makes a folder named logs and then, only if that worked, moves into it with cd.Tap to reveal
mkdir logs && cd logs
pwd
/home/asha/shop/logs
mkdir logs && cd logs.
Did you know?
127 means not found
Some exit statuses have fixed meanings in bash. Its manual says that if a command is not found, the process made to run it returns a status of 127, and if a command is found but is not executable, the status is 126. So echo $? after a typo shows 127, a clue that the shell could not find what you typed.
Why it fits here: It answers this topic's question: a command that does not exist leaves the exit status 127.
130 after Ctrl+C
A command stopped by a signal leaves a status that names the signal. The bash manual says that when a command ends on a fatal signal numbered N, bash uses 128+N as its exit status. Ctrl+C sends SIGINT, the interrupt from the keyboard, which is signal 2, so a command stopped with Ctrl+C leaves the status 130.
Why it fits here: It ties this topic's exit statuses to the keys: a status above 128 means a signal stopped the command.
Sources: GNU Bash Reference Manual: Exit Status; Linux man-pages: signal(7)
In one line
Read an error from the left: who complained, about what, and why. Every command leaves an exit status in $?: 0 is success, and anything else is failure. Join commands with ; to go on regardless, && to go on after success, and || to go on after failure.
The keys that save typing
At a glance
A good typist does not retype a whole letter to fix one word. The shell gives keys that save typing in the same spirit.
Tab finishes a name and checks that it exists, and the up arrow brings back earlier commands. Ctrl+C stops a running command, or drops the line being typed. Ctrl+A jumps to the start of the line and Ctrl+E to its end. Pressing Tab twice lists every name that could follow. And in a terminal, Ctrl+C never means copy.
Take a guess: Where do the shortcuts Ctrl+A and Ctrl+E come from?
A terminal looks slow until you learn its keys. Then you type far less than the screen shows. These keys belong to a real terminal, so the blocks in this section have no Run button. Each was pressed in real bash, and the sessions below are what came back. Try them in any Linux terminal you can reach.
Tab finishes names
Press Tab after the first few letters of a command or a file, and bash finishes the name for you. Suppose a folder holds notes.txt. You type cat no, press Tab, and the line becomes:
Typed: cat no, then Tab, then Enter
asha@tower:~/shop$ cat notes.txt
three pumps
asha@tower:~/shop$
Tab is not only faster. It also checks your spelling: if bash cannot finish a name, the name is not there. If two names start the same way, Tab fills in only what they share. With notes.txt and notebook.txt, cat no becomes cat note and stops. Press Tab twice more and bash lists both, so you can type the next letter.
The arrows bring back old commands
The up arrow brings back the last command, and each press goes one further back. The down arrow comes forward again. You can then edit the line, or press Enter to run it again:
Typed: echo one, Enter, the up arrow, Enter
asha@tower:~/shop$ echo one
one
asha@tower:~/shop$ echo one
one
asha@tower:~/shop$
The list behind the arrows is the history. The command history prints it with numbers, and !! runs the last command again. Ctrl+R searches it: press Ctrl+R, type a few letters of an old command, and bash finds the latest match. Press Enter to run it.
Ctrl+C stops things
Ctrl+C means hold Ctrl and press C. It stops the command that is running. This one would otherwise sleep for 100 seconds:
Typed: sleep 100, Enter, then Ctrl+C
asha@tower:~/shop$ sleep 100
^C
asha@tower:~/shop$ echo $?
130
asha@tower:~/shop$
The ^C is how a terminal shows Ctrl+C. The status 130 says that the command was stopped by that key: bash adds 128 to the number of the signal, and this one is signal 2. The processes lesson explains signals.
Ctrl+C also drops a half-typed line and gives you a fresh prompt, which is the quick way out of a line you have given up on. It rescues you from the > prompt too. The shell shows > when your line is unfinished, as with a missing quote:
Typed: echo "hi, Enter, then Ctrl+C
asha@tower:~/shop$ echo "hi
> ^C
asha@tower:~/shop$ echo back
back
asha@tower:~/shop$
Ctrl+C does not copy here
In a terminal, Ctrl+C means stop, not copy. Most Linux terminals copy with Ctrl+Shift+C and paste with Ctrl+Shift+V. Pressing Ctrl+C to copy a line during a long job stops the job.
Moving along a line
The left and right arrows move one letter at a time. On a long line these are quicker:
| Keys | What they do | A way to remember |
|---|---|---|
| Ctrl+A | jump to the start of the line | A is the start of the alphabet |
| Ctrl+E | jump to the end of the line | E for end |
| Ctrl+U | clear from the cursor back to the start | U for undo the line |
| Ctrl+W | remove the word before the cursor | W for word |
| Ctrl+L | clear the screen, keeping your line | L for level, a clean page |
| Ctrl+R | search old commands | R for reverse search |
| Ctrl+D | on an empty line, leave the shell | D for done |
| Tab | finish a name | |
| up arrow | bring back the last command | |
| Ctrl+C | stop, or drop the line | C for cancel |
Learn four first
Tab, the up arrow, Ctrl+C and q cover most of a beginner's day. Add one more key each week, and your hands will learn it.
Worked example 5
You typed a long command and pressed Enter, and bash said it could not find cho, because you missed the first letter. Fix the line without typing it again.
What we have: the last command, cho hello, with one letter missing at the start
What we need: the same line, corrected, with as few keys as possible
The plan: bring the line back with the up arrow, jump to its start with Ctrl+A, type the letter, press Enter
Typed: the up arrow, Ctrl+A, e, Enter
asha@tower:~/shop$ cho hello
bash: cho: command not found
asha@tower:~/shop$ echo hello
hello
asha@tower:~/shop$
- The second prompt shows the mended line. Four key presses did the work of eleven.
The up arrow, Ctrl+A, e, Enter prints hello.
Check: the command now starts with echo, and it printed the word.
A path that fails: holding Backspace until the slip, then typing the rest again. That works, but it costs more keys and risks a new slip. On a long line, jump.
When you are stuck: the terminal seems frozen, or the keys do odd things
If there is no prompt, a command is still running: press Ctrl+C.
If you see > at the start of the line, the shell is waiting for the rest: press Ctrl+C.
If a screen of text is showing and keys do nothing useful, you may be in a reader such as less: press q.
If the window closed, you may have pressed Ctrl+D on an empty line: open a new terminal.
Your turn
You have typed half of a long command and changed your mind. What is the fastest way to a clean prompt?Tap to reveal
- Ctrl+C drops the line and gives a fresh prompt.
Ctrl+C.
A command is running and will not stop. What do you press, and what status does it leave?Tap to reveal
- Ctrl+C stops it.
- bash reports 128 plus the signal number, and Ctrl+C sends signal 2.
Ctrl+C, and the status is 130.
You type cat no and press Tab, and the line stops at cat note. Why did it not finish the name?Tap to reveal
- Tab fills in only what the matching names share.
- So at least two names start with note.
Two names start with note, so bash needs another letter from you.
Did you know?
Keys from Emacs
The shortcuts Ctrl+A and Ctrl+E come from a text editor. The bash manual says that by default its line editing commands are similar to those of Emacs, the editor whose keys they copy, and that a vi-style interface is also available. The command set -o vi switches bash to the keys of vi, and set -o emacs switches it back.
Why it fits here: It answers this topic's question: Ctrl+A and Ctrl+E are Emacs keys, which bash uses unless told otherwise.
Ctrl+D ends it all
Ctrl+D does two jobs. In the middle of a line it deletes the character at the cursor. On an empty line, the bash manual says, it is read as the end of input, and bash quits when input ends; its IGNOREEOF variable exists only to make it wait for several in a row. So Ctrl+D on an empty prompt closes the shell, much as exit does.
Why it fits here: It adds one more key to this topic's list: Ctrl+D on an empty line ends input, and so ends the shell.
Source: GNU Bash Reference Manual: Commands For Changing Text
In one line
Tab finishes names and checks them, the up arrow brings back old commands, and Ctrl+C stops a command or drops a line. Ctrl+A and Ctrl+E jump along a line. In a terminal, Ctrl+C never means copy.
Linux, the kernel and distributions
At a glance
A building has foundations, rooms built on them, and furniture that makes it a home. A Linux system is built in layers in the same way.
The kernel shares the hardware among programs, and Linux is a kernel. GNU supplies the shell and the basic tools. A distribution, such as Ubuntu, joins them into a complete system with an installer and updates. A command you type passes down through the layers to the hardware, and the answer comes back up.
Take a guess: Did the person who began Linux expect it to become big?
You have been told that this is Linux and that this is bash. Those are different things, and people mix them up. Picture a busy railway junction. Many trains want the same tracks and platforms, and no train decides for itself. The station master gives each one its turn.
A computer has the same problem. Many programs want the same processor, memory and disks, and something must share them out. That something is one special program, which starts first and keeps running until the machine stops.
The program at the heart of an operating system. It controls the hardware, and shares the processor, memory and disks among all the other programs. Programs never touch the hardware themselves: they ask the kernel.
Linux is a kernel. It was written from scratch by Linus Torvalds, with help from a loose team of people across the Internet, as a clone of an older system called Unix. That is why people call these systems Unix-like, and why much of this step also works on a Mac, whose system has Unix roots. Its code is free for anyone to read and change, under a licence called the GNU General Public License.
uname, short for Unix name, prints the name of the kernel you are running on:
uname
Linux
A kernel alone is not enough
A kernel has no ls, no shell and no window. Those come from other projects. The shell and most of the basic commands come from GNU, a free operating system project. Their version lines say so:
Terminal
asha@tower:~/shop$ ls --version | head -1
ls (GNU coreutils) 9.7
asha@tower:~/shop$ bash --version | head -1
GNU bash, version 5.3.9(1)-release (x86_64-pc-linux-gnu)
So what do you actually install? A distribution, or distro for short.
A complete system made by joining the Linux kernel with a shell, tools, a way to install programs, and often a desktop. Ubuntu and Debian are two well-known distributions.
A distribution chooses its pieces, and two distributions can differ in those choices while sharing a kernel. This lesson was written on Ubuntu 26.04, whose own ls is a newer rewrite called uutils:
On the laptop this lesson was written on
asha@tower:~/shop$ /usr/bin/ls --version | head -1
ls (uutils coreutils) 0.10.0
asha@tower:~/shop$ uname -r
7.0.0-38-generic
asha@tower:~/shop$
The lesson's blocks use GNU's tools, the ones most systems you meet will have, so the kit puts GNU's ls in that place. The second command, uname -r, prints the kernel's version number, which is different on nearly every machine.
The layers, from your hands to the metal
| Layer | An example | Its job |
|---|---|---|
| terminal | the Terminal app | shows text and takes your keys |
| shell | bash | understands your commands |
| programs | ls, wc, node | do the actual jobs |
| kernel | Linux | shares the hardware among programs |
| hardware | processor, memory, disk | does the physical work |
When you run ls, all of them take part. The terminal sends your keys to bash. Bash starts the program ls. ls asks the kernel what is in the folder. The kernel reads the disk, and the answer travels back up to your screen.
You will meet Linux in many places. Servers are the usual home of it. Android phones are built on the Linux kernel. Microsoft's WSL runs distributions such as Ubuntu and Debian on a Windows machine. Learning it once serves you in all of them.
Worked example 6
Warden Rao hands you a list and asks you to sort it into layers: Ubuntu, bash, Linux, wc, Terminal. One of them is not a layer at all.
What we have: five names, and the table of layers
What we need: each name matched to its layer
The plan: go through the jobs: who shows text, who understands commands, who does a job, who shares the hardware
- Terminal shows text and takes keys, so it is the terminal layer.
- bash understands commands, so it is the shell.
- wc does one job, so it is a program.
- Linux shares the hardware, so it is the kernel.
- Ubuntu is none of these: it is the whole package, a distribution, which holds a kernel, a shell and programs together.
uname
type wc
type cd
Linux
wc is /usr/bin/wc
cd is a shell builtin
- The commands back it up: uname names the kernel, and type shows a program and a part of the shell.
Ubuntu is the odd one out: it is a distribution, which contains the others.
Check: ask whether each thing could be swapped alone: you can change your shell without changing your distribution.
A path that fails: saying that Linux is the whole operating system. In everyday speech people do, and that is fine, but the word strictly names the kernel. It matters when a question is about the kernel's version and not the distribution's.
When you are stuck: the words Linux, Ubuntu and bash blur together
Linux is the kernel, the part that runs the hardware.
Ubuntu is a distribution, the whole package.
bash is a shell, one program inside that package.
Ask what each one does, and the layer table tells you which it is.
Your turn
uname
uname -s
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Linux
Linux
- uname prints the kernel's name.
- -s asks for the kernel's name too, which is what uname prints with no options.
It prints Linux twice.
Which layer shares the processor and memory among programs?Tap to reveal
- Programs ask for the hardware; they do not take it.
The kernel.
A friend says, "I installed Linux." What did they actually install, and what is inside it?Tap to reveal
- Nobody installs a bare kernel on a laptop.
- They installed a distribution.
A distribution, which holds the Linux kernel, a shell, tools and more.
Did you know?
GNU, announced in 1983
The GNU in a Linux system began with a newsgroup post. On 27 September 1983 Richard Stallman announced: starting this Thanksgiving I am going to write a complete Unix-compatible software system called GNU, for Gnu's Not Unix, and give it away. The shell and basic tools that this lesson types into come from that project.
Why it fits here: It explains the GNU in this topic's layers: the shell and the basic tools come from a project announced in 1983.
Just a hobby
Linux began as a hobby, in its author's own words. On 25 August 1991 Linus Torvalds, at the University of Helsinki, posted to the comp.os.minix newsgroup: I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. Wikipedia gives Torvalds's age at the time as 21.
Why it fits here: It answers this topic's question: the Linux kernel began as a student's hobby, announced in 1991.
Sources: Wikipedia: History of Linux; Carnegie Mellon University: Linux history (the original posts)
In one line
The kernel shares the hardware among programs, and Linux is a kernel. GNU supplies the shell and basic tools. A distribution, such as Ubuntu, joins them into a complete system. Your command passes down through the layers, and the answer comes back up.
Where it is used
Everything in this lesson returns in every later step. Here is where each idea does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| a command with options and arguments | every developer tool | git commit -m "fix", npm run build |
| --help and man | any tool you have not met before | docker --help |
| reading errors | every failed install or build | a build log that stops at one red line |
| the exit status | automatic checks | a test run that must end in 0 |
| && between commands | steps that depend on each other | npm test && git push |
| Tab and the arrows | daily work | long paths on a server |
| Linux | servers | a machine with no screen, reached over a network |
Mistakes and debugging
These are the slips that people meet most often in their first week. Every output below is the real one, from the code shown.
1. Copying the prompt
$ pwd
bash: line 1: $: command not found
Tutorials show a $ in front of commands, and it is not part of them. The fix: copy what comes after the prompt.
2. A capital letter
Echo hi
bash: line 1: Echo: command not found
Names on Linux are exact. The fix: echo hi.
3. No space before an option
ls-l
bash: line 1: ls-l: command not found
With no space, the shell sees one word. The fix: ls -l.
4. A name with a space in it
In a folder with a file named my notes.txt
cat my notes.txt
cat "my notes.txt"
cat: my: No such file or directory
cat: notes.txt: No such file or directory
first line
Without quotes, cat receives two names, and neither exists. The fix: quote the name, or press Tab and let bash write it for you.
5. One dash on a long option
In a folder with notes.txt
wc -lines notes.txt
wc: invalid option -- 'i'
Try 'wc --help' for more information.
One dash means single letters, so wc took l, then choked on i. The fix: wc --lines notes.txt, or wc -l notes.txt.
6. An unfinished quote
echo "hello
bash: -c: line 1: unexpected EOF while looking for matching `"'
On this page the small script ends in the middle of the quote, so bash reports it. In a real terminal you get a > prompt, and bash waits for the rest. The fix: close the quote, or press Ctrl+C and type the line again.
7. Going on after a failure
In a folder with notes.txt, and no folder named backup
cd backup; rm notes.txt
ls
bash: line 1: cd: backup: No such file or directory
This is the costly one. cd failed, so the shell never moved. The semicolon ran rm anyway, in the wrong folder, and it removed the real notes.txt. The ls at the end prints nothing, because the folder is now empty. The fix: join with &&, so that rm runs only if cd worked:
The same folder, with &&
cd backup && rm notes.txt
ls
bash: line 1: cd: backup: No such file or directory
notes.txt
The debugging corner: slow down and read
Most shell problems come from one wrong word. Run the failing part alone. Read the first error from the left. Check pwd to see where you are, and ls to see what is there. Then ask the command with --help. That order solves nearly everything in this lesson.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
The lesson in code
pwd # which folder am I in?
whoami # who am I?
ls -la | wc -l # list everything, and count the lines
echo "two spaces" # quotes keep spaces
wc --lines notes.txt # a long option
wc -l notes.txt # its short form
type cd # a builtin, or a program?
uname # which kernel?
ls nothing.txt # this one fails
echo $? # its exit status
false && echo skipped # && needs success
false || echo fallback # || runs after failure
false; echo always # ; runs regardless
/home/asha/shop
asha
4
two spaces
3 notes.txt
3 notes.txt
cd is a shell builtin
Linux
ls: cannot access 'nothing.txt': No such file or directory
2
fallback
always
| When you see | It means | Look at |
|---|---|---|
command not found | no command has that name | spelling, capitals, a copied $ |
No such file or directory | that name is not here | pwd, ls, and quotes |
invalid option -- 'i' | a letter that is not an option | one dash or two; --help |
missing operand | it needs an argument | the Usage line |
| a > prompt | your line is unfinished | a missing quote; Ctrl+C |
| no prompt | a command is still running | Ctrl+C |
| a screen of text with no prompt | a reader such as less | q |
| a status of 0 | success | nothing to fix |
| a status of 127 | the command was not found | its name |
| a status of 130 | stopped by Ctrl+C | nothing to fix |
The story: One exact word
You learn the form of it. A word that names the job comes first, then small marks that change how it is done, then what it is done to. The tube tells you where you stand and who it takes you for. When you get a word wrong it says so, and it tells you plainly, and you say it again. At last you ask the tube for the gate's own instructions, and read them.
You speak one exact line, and the gate swings open.
Warden Rao trims her lamp. "Now you can speak," she says. "Next, learn where you are."
Reagents gathered: terminal, shell, prompt, option, exit status, kernel.
Sources
- GNU Bash Reference Manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)
- GNU Coreutils manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)
- The Linux kernel documentation (admin guide, README) (GPL-2.0, the kernel's own licence (the README, read 2026-11-06); facts and ideas only)