Bash is very widespread on Linux and remains the reference shell in most administration guides. This guide takes you from "I see a black screen" to "I type commands, I fix them quickly and I understand what the terminal tells me". No prior knowledge is required, only access to a Linux machine (physical, VM or WSL).
What you will learn
- Open a terminal and read the prompt
- Type, fix and recall commands
- Use the essential Bash keyboard shortcuts
- Understand exit codes and error messages
- Find where a command lives and read its help
How do you open a terminal?
The terminal is the graphical application that hosts your shell. The shell is the program that interprets your commands.
Keyboard shortcut: Ctrl + Alt + T. You can also search for "Terminal" in the applications.
Keyboard shortcut: Ctrl + Alt + T, or open Konsole from the menu.
From another computer, connect to the machine:
ssh user@ip-addressYou land directly in a Bash shell (if it is the account's default shell).
Once the terminal is open, you see the prompt: it is the line that waits for you to type something.
How do you read the prompt?
The default Bash prompt looks like this. Each part carries information about who you are and where you are:
bob@server:~$Read it element by element:
| Element | Meaning |
|---|---|
bob | Name of the logged-in user |
@ | Separator |
server | Name of the machine (hostname) |
: | Separator |
~ | Current directory (~ = home directory, /home/bob) |
$ | You are a regular user (# if you are root) |
The last sign of the prompt deserves a glance before every command. A final
# signals a root session, so an administrator one: at that level,
no command asks for confirmation and nothing prevents a permanent deletion.
Work as a regular user unless you explicitly need otherwise, and switch to
root for a single command with sudo.
Check which shell is active
The $SHELL variable shows the login shell configured for your account,
but not necessarily the one currently running. To check the shell that is
really active:
echo "$0"Or, in more detail:
ps -p $$ -o comm=If the result is bash, you are indeed in Bash.
How do you type your first command?
A command is a word you type, possibly followed by options and arguments, then confirmed with Enter.
-
Type
whoamithen press Enter:Terminal window whoamiThe shell shows your user name.
-
Type
pwd(print working directory):Terminal window pwdThe shell shows the full path of the directory you are in.
-
Type
date:Terminal window dateThe shell shows the system date and time.
Every command follows the same logic, in a fixed order, which the shell always reads from left to right:
command [options] [arguments]For example, ls -l /etc calls the ls command, with the -l option (long
format) and the /etc argument (the directory to list).
A trap shows up from the very first command: Linux distinguishes upper case
from lower case, everywhere. ls works, LS does not, and the
message you get never mentions case: it only says the command cannot be
found. The rule also applies to file and directory names.
How do you move through the history?
Bash keeps the commands you have typed, so you do not need to retype everything each time. The history is also searchable, which matters more than the arrow keys once it gets long.
| Action | Key |
|---|---|
| Previous command | Up Arrow |
| Next command | Down Arrow |
| Search for a command | Ctrl + R then type a word |
| Show the whole history | history |
| Rerun command no. 42 | !42 |
| Rerun the last command | !! |
Try it: type a few commands (whoami, date, pwd), then press
Up Arrow several times to get them back.
Interactive search with Ctrl + R
The arrow keys are fine for going back a few commands, not for finding a line typed last week. Incremental search walks through the whole history from a fragment: a few letters are enough, and Bash shows the most recent match as you type.
-
Press Ctrl + R. The prompt changes:
(reverse-i-search)`': -
Type the first letters of the command you are looking for, for example
who. Bash shows the most recent match. -
Press Enter to run it, or Ctrl + R again to go further back in the history.
-
To cancel the search, press Ctrl + C.
Which keyboard shortcuts should you know?
By default, Bash relies on Readline, the library that handles line
editing, with Emacs-style shortcuts. This behaviour can be customised
through ~/.inputrc, but the shortcuts below work in the vast majority of
cases.
Moving within the line
These 4 shortcuts save you from holding down an arrow key to get back to the start of a long command. Learn the first 2 first, which cover most needs: Ctrl + A for the start of the line, Ctrl + E for the end. Word-by-word movement comes next, once those two have become automatic.
| Shortcut | Action |
|---|---|
| Ctrl + A | Go to the start of the line |
| Ctrl + E | Go to the end of the line |
| Alt + F | Move forward one word |
| Alt + B | Move back one word |
Deleting and fixing
Fixing a command almost always costs less than retyping it, and that is what these 5 shortcuts are for. The last one is worth knowing before the others: Ctrl + Y pastes back the text you have just deleted, which rescues a Ctrl + U pressed too quickly.
| Shortcut | Action |
|---|---|
| Ctrl + U | Delete from the cursor to the start of the line |
| Ctrl + K | Delete from the cursor to the end of the line |
| Ctrl + W | Delete the word before the cursor |
| Alt + D | Delete the word after the cursor |
| Ctrl + Y | Paste the deleted text back (yank) |
Control
These four shortcuts act on the running command or on the session. Keep Ctrl + C in mind first: it is the one that stops a command that takes too long.
| Shortcut | Action |
|---|---|
| Ctrl + C | Interrupt the running command |
| Ctrl + D | On an empty line, signals the end of input (EOF) and can close the shell |
| Ctrl + L | Clear the screen (same as clear) |
| Ctrl + Z | Suspend the running command (resume with fg) |
A special case is worth knowing: if the IGNOREEOF variable is set,
Ctrl + D no longer closes the shell on the first press, contrary to what every cheat sheet
says. That is precisely why exit remains the reliable way to leave a
session, whatever the configuration of the machine.
The best way to build these reflexes is to try them out on nothing. Type a long dummy command, move around with Ctrl + A and Ctrl + E, delete a word with Ctrl + W. A few minutes are enough, and the gain then shows on every line you type.
How does Tab completion work?
The Tab key is the most important shortcut in Bash: it completes commands and paths, and saves you from typos at the same time.
-
Type
cat /etc/hostthen press Tab:Bash completes it to
cat /etc/hostname(or offers youhostsandhostnameif several files match). -
Press Tab twice if nothing happens: Bash shows every possibility.
-
Completion also works on commands: type
systhen Tab Tab to seesystemctl,sysctl, and so on.
Completion does not stop at paths and commands. On recent distributions, it
also covers service names, where systemctl restart ng followed by Tab
offers nginx, and package names. If nothing happens, the
bash-completion package is probably missing from the installation.
How do you read an exit code?
Every command ends with an exit code. It is a number that the shell keeps and that tells whether the command succeeded or failed.
| Code | Meaning |
|---|---|
0 | Success |
1 | Generic error |
2 | Misuse of the command |
126 | Command found but not executable (permissions) |
127 | Command not found |
130 | Interrupted by Ctrl + C |
To see the exit code of the last command, print the $? variable:
echo $?A concrete example, with a success then a failure:
ls /etc/hostnameecho $?# 0: the file exists, the command succeeded
ls /nonexistent-fileecho $?# 2: error, the file does not existHow do you find where a command lives?
To understand what Bash runs when you type a word, use the
type builtin:
type ls# ls is aliased to `ls --color=auto'
type cd# cd is a shell builtin
type -a grep# grep is /usr/bin/greptype tells you whether a word is an alias, a shell builtin,
a function or an executable on disk. The -a option lists every
possible resolution.
Reading a command's help
For an executable, use man, which opens the command's manual page:
man lsFor a shell builtin, man does not always have a page.
Use help instead:
help cdhelp historyhelp typehelp documents the commands that Bash runs itself, without starting an
external program.
Which commands help you explore the system?
Now that you know the prompt, the history and the shortcuts, here are a few commands for your first explorations:
| Command | What it does |
|---|---|
ls | Lists the files in the current directory |
ls -la | Lists with hidden files and details |
cd /var/log | Changes directory |
cd | Goes back to the home directory |
cat /etc/os-release | Shows the contents of a file |
head -5 /etc/passwd | Shows the first 5 lines |
echo "Hello" | Prints some text |
uname -a | Information about the kernel |
uptime | How long the system has been running |
free -h | Available memory (readable format) |
df -h | Disk space per partition |
How do you read the first errors?
When you make a mistake, Bash prints a message. Here are the most common errors at the very beginning:
| What you type | Message | What happened |
|---|---|---|
Whoami | command not found | The case is wrong |
ls /missing-folder | No such file or directory | The path does not exist |
cat /root/secret | Permission denied | You do not have the rights |
cd /etc/passwd | Not a directory | It is a file, not a directory |
LS | command not found | Case matters on Linux |
The reflex to build right away is to read the message before retyping. It almost always names the exact cause among the cases in the table above: wrong name, missing path, or insufficient permissions. Retyping at random costs far more time than reading one line.
How do you copy and paste in the terminal?
The terminal does not use the same shortcuts as the rest of the desktop: Ctrl + C is already taken to interrupt a command.
| Action | Shortcut |
|---|---|
| Copy | Ctrl + Shift + C |
| Paste | Ctrl + Shift + V |
Quick exercise, 3 minutes
Put what you have just read into practice, in this order. Each step reuses a command or a shortcut from the guide:
-
Open a terminal.
-
Check your name with
whoami. -
Show the current directory with
pwd. -
List the files with
ls. -
Look at your history with
history. -
Recall a previous command with Up Arrow, then confirm it.
-
Interrupt a long command with Ctrl + C (try
sleep 60then Ctrl + C). -
Leave the shell with
exit.
Key points
- The prompt tells you who you are, where you are and whether you are root.
- Every command takes the form
command [options] [arguments]then Enter. - Tab completes commands and paths: use it all the time.
- Ctrl + R finds a past command in a few keystrokes.
- Ctrl + C interrupts, Ctrl + L clears the screen, Ctrl + A/E move within the line.
echo $?gives the exit code: 0 = success, anything else = error.typeidentifies what Bash runs;helpdocuments the builtins.exitcloses the shell session cleanly.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Linux environment variables
A new full quiz with random questions
Work again only on the questions you missed