Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Getting started with Bash

By updated

Improve this page

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.

ActionKey
Previous commandUp Arrow
Next commandDown Arrow
Search for a commandCtrl + R then type a word
Show the whole historyhistory
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.

  1. Press Ctrl + R. The prompt changes:

    (reverse-i-search)`':
  2. Type the first letters of the command you are looking for, for example who. Bash shows the most recent match.

  3. Press Enter to run it, or Ctrl + R again to go further back in the history.

  4. 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.

ShortcutAction
Ctrl + AGo to the start of the line
Ctrl + EGo to the end of the line
Alt + FMove forward one word
Alt + BMove 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.

ShortcutAction
Ctrl + UDelete from the cursor to the start of the line
Ctrl + KDelete from the cursor to the end of the line
Ctrl + WDelete the word before the cursor
Alt + DDelete the word after the cursor
Ctrl + YPaste 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.

ShortcutAction
Ctrl + CInterrupt the running command
Ctrl + DOn an empty line, signals the end of input (EOF) and can close the shell
Ctrl + LClear the screen (same as clear)
Ctrl + ZSuspend 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.

  1. Type cat /etc/os-r then press Tab:

    Bash completes it to cat /etc/os-release, since only one name starts that way.

  2. Type cat /etc/host then press Tab twice: several names start with these letters, so Bash shows them all, for example hostname and hosts. Add a letter and press Tab to continue.

  3. Completion also works on commands: type sys then Tab Tab to see systemctl, 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:

Terminal window
echo $?

A concrete example:

Terminal window
ls /etc/hostname
echo $?
# 0: the file exists, the command succeeded
ls /nonexistent-file
echo $?
# 2: error, the file does not exist

Find where a command lives

To understand what Bash runs when you type a word, use the type builtin:

Terminal window
type ls
# ls is aliased to `ls --color=auto'
type cd
# cd is a shell builtin
type -a grep

type 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:

Terminal window
man ls

For a shell builtin, man does not always have a page. Use help instead:

Terminal window
help cd
help history
help type

help 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:

CommandWhat it does
lsLists the files in the current directory
ls -laLists with hidden files and details
cd /var/logChanges directory
cdGoes back to the home directory
cat /etc/os-releaseShows the contents of a file
head -5 /etc/passwdShows the first 5 lines
echo "Hello"Prints some text
uname -aInformation about the kernel
uptimeHow long the system has been running
free -hAvailable memory (readable format)
df -hDisk 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:

  1. Open a terminal.

  2. Check your name with whoami.

  3. Show the current directory with pwd.

  4. List the files with ls.

  5. Look at your history with history.

  6. Recall a previous command with Up Arrow, then confirm it.

  7. Interrupt a long command with Ctrl + C (try sleep 60 then Ctrl + C), then type echo $?: you read 130.

  8. Leave the shell with exit.

Key points

  • The arrows and history recall 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, including 130 when Ctrl + C interrupted the command.
  • type identifies what Bash runs; help documents the builtins.
  • exit leaves 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

6 questions
6 min
70% to pass

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