Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

By updated

Improve this page

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.

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:

ElementMeaning
bobName of the logged-in user
@Separator
serverName 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:

Terminal window
echo "$0"

Or, in more detail:

Terminal window
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.

  1. Type whoami then press Enter:

    Terminal window
    whoami

    The shell shows your user name.

  2. Type pwd (print working directory):

    Terminal window
    pwd

    The shell shows the full path of the directory you are in.

  3. Type date:

    Terminal window
    date

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

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

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.

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

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.

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 session. 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)

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.

  1. Type cat /etc/host then press Tab:

    Bash completes it to cat /etc/hostname (or offers you hosts and hostname if several files match).

  2. Press Tab twice if nothing happens: Bash shows every possibility.

  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.

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.

CodeMeaning
0Success
1Generic error
2Misuse of the command
126Command found but not executable (permissions)
127Command not found
130Interrupted by Ctrl + C

To see the exit code of the last command, print the $? variable:

Terminal window
echo $?

A concrete example, with a success then a failure:

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

How do you 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
# grep is /usr/bin/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.

Reading a command's help

For an executable, use man, which opens the command's manual page:

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.

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:

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

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 typeMessageWhat happened
Whoamicommand not foundThe case is wrong
ls /missing-folderNo such file or directoryThe path does not exist
cat /root/secretPermission deniedYou do not have the rights
cd /etc/passwdNot a directoryIt is a file, not a directory
LScommand not foundCase 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.

ActionShortcut
CopyCtrl + Shift + C
PasteCtrl + 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:

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

  8. 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.
  • type identifies what Bash runs; help documents the builtins.
  • exit closes 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

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

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn