Bash is the shell you use hundreds of times a day, and a few reflexes save a considerable amount of time in it. This page teaches you to work fast in Bash: recall a command from the history, fix a line without retyping it, complete with Tab, read the exit code of a command and know where what Bash runs comes from. It assumes that you already know how to open a terminal and read the prompt.
What you will learn
- Recall and search commands in the history
- Use the essential keyboard shortcuts of Bash
- Complete commands and paths with Tab
- Read the exit code of a command
- Know where a command lives and read its help
Before you start: an open terminal and a readable prompt
Opening a terminal, reading the prompt (bob@server:~$), checking which shell is
active and typing your first commands are gestures described in
Open a terminal and understand the prompt.
The shape of a command (command [options] [arguments]) is explained in
Anatomy of a Linux command.
If you have never used a terminal, read these two pages first: the examples
below assume that you can type a command and read an error.
Move through the history
Bash keeps the commands you have typed. You do not need to retype everything each time.
| 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. The 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.
Essential keyboard shortcuts
By default, Bash relies on Readline, the library that handles line editing.
Its shortcuts are those of Emacs, a text editor widely used by administrators.
The ~/.inputrc file lets you customise them, but the shortcuts below work in
the vast majority of cases.
Move within the line
These four shortcuts save you from holding down an arrow key to get back to the start of a long command. Learn the first two 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 |
Delete and fix
Fixing a command almost always costs less than retyping it, and that is what these five 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 shell. 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) |
Ctrl + D does not always close the shell. If the IGNOREEOF variable is set,
Ctrl + D no longer closes the shell on the first press: you have to repeat
it.
After a Ctrl + Z, as long as a command stays suspended, Bash may also refuse
to close and print There are stopped jobs. Resume the command with fg, or
finish it, before you quit.
exit does not depend on IGNOREEOF: it is the method to remember for leaving
the shell. The details are in
Open a terminal and understand the prompt.
To build these reflexes, try them out on nothing. Type a long dummy command, move around with Ctrl + A and Ctrl + E, then delete a word with Ctrl + W. A few minutes are enough, and the gain then shows on every line you type.
Tab completion
The Tab key completes what you have started to type: a command, file or directory name. It saves you from typos, and it shows you what exists before you have to guess.
-
Type
cat /etc/os-rthen press Tab:Bash completes it to
cat /etc/os-release, since only one name starts that way. -
Type
cat /etc/hostthen press Tab twice: several names start with these letters, so Bash shows them all, for examplehostnameandhosts. Add a letter and press Tab to continue. -
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.
Understand exit codes
Every command ends with an exit code. It is a number that the shell keeps and
that tells whether the command succeeded or failed. The codes 0, 1, 2,
126 and 127 are described in
Anatomy of a Linux command.
Only one is added here: 130 means that the command was interrupted by
Ctrl + C (it is 128 plus 2, the number of the interrupt signal).
To see the exit code of the last command:
echo $?A concrete example:
ls /etc/hostnameecho $?# 0: the file exists, the command succeeded
ls /nonexistent-fileecho $?# 2: error, the file does not existFind 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 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: for grep,
the command shows each place where Bash finds that name, an alias if there is one,
then the path of the executable. The output depends on your distribution, because
some define an alias for grep and others do not.
Read a command's help
For an executable, use man:
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.
A few commands to 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 |
Understand the first errors
The table of the most common errors at the beginning (command not found,
No such file or directory, Permission denied, Not a directory) is in
Open a terminal and understand the prompt.
When you go fast, keep a single reflex: read the whole message before
retyping the command.
To copy and paste in the terminal without running a command by mistake, the shortcuts and the precaution to take are in the lesson Open a terminal and understand the prompt.
Quick exercise, 3 minutes
Put what you have just read into practice:
-
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), then typeecho $?: you read130. -
Leave the shell with
exit.
Key points
- The arrows and
historyrecall a command; Ctrl + R finds it from a few letters. - Tab completes commands and paths: use it all the time.
- 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, including130when Ctrl + C interrupted the command.typeidentifies what Bash runs;helpdocuments the builtins.exitleaves the shell 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)Result saved on this device. It will be sent to your account at the next sync. Sync now
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