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

Open a terminal and understand the prompt

By updated

Improve this page

The terminal is your way into Linux. It is where you install a package, read a log or start a service, often on a machine with no graphical interface. This guide assumes a Linux machine, desktop or server, and no experience with the terminal: you open it, read what it displays and type your first commands, in under ten minutes. The history, completion and shortcuts of Bash have their own guide: Getting started with Bash.

What you will learn

  • Open a terminal on a graphical desktop or over SSH
  • Read the prompt and identify the user, the machine and the current directory
  • Tell apart terminal, shell and session
  • Run discovery commands: whoami, pwd, hostname, date
  • Recognise the first error messages
  • Copy and paste safely and leave the terminal cleanly

Terminal, shell, session: three words to tell apart

Three words come up constantly as soon as you talk about the terminal. It is better to tell them apart before typing any command.

WordWhat it is
TerminalThe graphical application (or the SSH window) that displays text
ShellThe program that interprets your commands (Bash, Zsh, Fish…)
SessionA user's connection to the machine, from opening (login) to closing (logout), for example over SSH or on the console

When you open a terminal, it starts a shell. The shell waits for your commands and runs them. When you close the terminal or type exit, the shell ends. The session only ends if that shell was the one of an SSH connection or of a console: in a graphical terminal window, your desktop session stays open.

The diagram follows a command from your keyboard to the screen. Look at each one's role: the terminal passes on what you type and displays text, whereas the shell is the only one that reads the line and starts the requested program. The program writes its output directly to the terminal, without going through the shell. The shell waits for the program to finish, receives its exit code (a number that says whether the command succeeded), then displays the next prompt.

Sequence diagram: you type a command then Enter in the terminal. The terminal passes the line on to the shell, which starts the requested program. The program writes its output directly to the terminal, which displays it. When the program ends, it returns its exit code to the shell, and the shell displays the next prompt in the terminal.ProgramShellTerminalYoutype a commandthen Enterpasses the line onstarts the programwrites its outputdisplays the textends, exit codedisplays the next promptyoulink in the chainthe command moves forwardtext comes backthe program exits
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

Open a terminal

How you open it depends on your machine: a shortcut or a menu on a graphical desktop, an ssh command for a remote machine. Choose the tab that matches your case.

On Ubuntu, the keyboard shortcut is Ctrl + Alt + T. It does not always exist on other GNOME distributions: search for "Terminal" in the applications instead.

If you work on Windows, the guide Install WSL2 gives you a Linux terminal in which the commands of this page work.

Once the terminal is open, you see a line of text waiting for your input: that is the prompt.

Understand the prompt

The default Bash prompt on Debian and Ubuntu looks like this:

bob@server:~$

Each part has a precise role:

ElementMeaning
bobName of the logged-in user
@Separator
serverName of the machine (hostname)
:Separator
~Current directory (~ = home directory, /home/bob)
$Regular user (# if you are root)

The current directory is the folder in which your commands run. It changes when you move around the folders.

That last sign deserves a glance before every command. If the prompt ends with #, you are logged in as root, the administrator. Stay a regular user unless you explicitly need otherwise: a mistake as root asks for no confirmation and can break the system. To run a single command with root rights, the guide Use sudo: run a command as root explains how to do it without switching accounts.

On RHEL, the default prompt looks like [bob@server ~]$: the same information, inside square brackets and without the colon, and only the name of the current directory is shown, not its full path. The prompt can be customised with the PS1 variable, as shown in Customise Bash: .bashrc, prompt, bash-it and fzf. A customised prompt may no longer show the user or the directory: the whoami command then tells you which account you are acting as, and pwd which directory you are in.

Check which shell is active

The $SHELL variable (an environment variable: a name to which the shell attaches a value) holds the default shell of your account, the one it starts at login (its login shell). It is not necessarily the one you are using right now: if you type zsh from Bash, $SHELL still says Bash. Two commands show the shell actually running. The first prints the name under which it was started:

Terminal window
echo "$0"

In a login shell, the kind an SSH session or a login on the console opens, the result starts with a dash: -bash. In a graphical terminal window you will usually read bash, without the dash.

The second command gives the bare name, whatever the way it was started. It queries the process table (the list of running programs):

Terminal window
ps -p $$ -o comm=

Here $$ is the process number (PID) of the current shell: ps therefore prints the name of the program that carries this number. If the result is bash, zsh or fish, that is the shell you are working in right now.

Type your first commands

A command is a word typed on the keyboard and confirmed with Enter. The shell starts the matching program, and its result is displayed in the terminal. The options and arguments that can follow it are explained in Anatomy of a Linux command.

  1. Who am I?

    Terminal window
    whoami

    Displays the name of the logged-in user (e.g. bob).

  2. Where am I?

    Terminal window
    pwd

    Displays the full path of the current directory (e.g. /home/bob). This is the path that the prompt shortens to ~ when you are in your home directory.

  3. On which machine?

    Terminal window
    hostname

    Displays the name of the machine.

  4. What date and time is it?

    Terminal window
    date

    Displays the system date and time.

  5. Which distribution?

    Terminal window
    cat /etc/os-release

    cat displays the content of a file. Here, /etc/os-release describes the installed distribution: its name and its version.

  6. Which kernel?

    Terminal window
    uname -r

    Displays the version of the kernel, the core program of Linux, the one that drives the hardware.

A trap shows up from the very first command: Linux distinguishes upper case from lower case, in command names as well as in file names. whoami works, Whoami does not, and the message you get says nothing about case: it only says the command cannot be found.

Understand the first errors

When you make a mistake, Bash displays a message. These are the most common errors at the very beginning:

What you typeMessageWhat happened
Whoamicommand not foundNo command with that name: wrong case or typo, or the program is missing
ls /missing-directoryNo 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

Read the whole message before typing the command again. It almost always says what is wrong: an incorrect name, a path that does not exist, a missing right. Retyping at random wastes far more time than reading one line. Depending on the language configured on the machine, these messages may appear in another language, such as French, rather than in English: their meaning stays the same.

Copy and paste in the terminal

The terminal does not use the same shortcuts as the rest of the desktop.

ActionShortcut
CopyCtrl + Shift + C
PasteCtrl + Shift + V

One precaution matters when pasting. From Bash 5.1 on, bracketed paste is enabled by default: the shell inserts the pasted text without running it, and nothing is sent before you press Enter. An older shell, or a terminal that does not support this mode, on the other hand runs each line ending with a line break as soon as it is pasted. So always reread what you paste, especially when it comes from a web page: an extra command can hide at the end of the block.

Leave the terminal

Two ways to close the shell cleanly. Over an SSH connection or on a console, this also closes the session:

Terminal window
exit

Or press Ctrl + D on an empty line: this shortcut tells the shell that there is nothing more to read (an end of input, or "end of file"), and the shell closes because it has no command left to wait for. It also works for a command that reads your input, such as cat run without an argument.

Ctrl + D does not always close the shell. If the IGNOREEOF variable is set, Bash requires several consecutive Ctrl + D, 10 if the variable has no numeric value. That is why exit remains the reliable method, whatever the configuration of the machine.

Do not confuse Ctrl + D with Ctrl + C, which interrupts the running program without closing the shell. They are two different actions, and interrupting a program can leave things half done. The box below, optional on a first pass, explains why Ctrl + D does not act like Ctrl + C.

Put it into practice

Reading the prompt in a guide and reading it on your own machine are not the same exercise. The lab prepares a separate working folder on your machine, with a premiers-pas.txt file to fill in. The commands act on your own machine: you find your user (whoami), the machine name (hostname), the date (date) and your home directory (echo $HOME, which the lab gives you), then write these four values into the file. The validation compares them with the real state of your session.

Key points

  • The terminal is the interface, the shell is the program that interprets your commands.
  • The prompt tells you who you are, on which machine and in which directory.
  • The prompt ends with $ for a regular user and with # for root.
  • whoami, pwd, hostname, date: four commands to find out where you stand.
  • Linux is case-sensitive: whoami exists, Whoami does not.
  • Read the error messages: they almost always explain the problem.
  • Ctrl+D signals the end of input and usually closes the shell on an empty line; Ctrl+C interrupts the running program.
  • exit closes the shell cleanly, and the session if it was an SSH connection or a console.

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