Lesson 1 of 18 · User
The terminal, the shell and the prompt
Objective
Know what happens when you type a command: who reads it, who runs it, and how to read the line waiting for you on the screen. This is the entry point to the rest of the course.
Key points
- The terminal is the window; the shell is the program that reads your commands (on Linux, almost always bash).
- The prompt tells you who you are, on which machine and in which directory. It ends with
$(normal user) or#(root, the administrator). - A command is written as
command options arguments, separated by spaces. - Linux is case-sensitive:
lsworks,LSdoes not exist. - The Tab key completes names, the up arrow recalls previous commands, Ctrl+C stops a command.
1. Terminal, console, shell: three words, three things
Picture a supermarket checkout:
- the screen and keyboard of the checkout are the terminal: they display things and receive what you type;
- the cashier who understands "two loaves of bread and a litre of milk" is the shell: she interprets your request;
- the warehouse that supplies the products is the Linux kernel: it does the real work (reading the disk, launching programs).
A server often has no graphical interface at all. You connect to it remotely (with ssh, which we will see later) and you land directly in a shell. That is why the command line is the core skill of an administrator.
There are several shells: sh (the historical shell, standardised by POSIX), bash (the most common on Linux), zsh (the default on macOS), dash (a very light sh used by Debian and Ubuntu for system scripts). In this course, we use bash: it is available on Red Hat, Debian and Ubuntu.
echo $SHELL # your account's login shell, for example /bin/bash
echo $0 # the shell running right now, for example -bash or bash
cat /etc/shells # the list of shells installed on the machine
2. Reading the prompt
When the shell is waiting for a command, it displays a prompt. On most distributions, it looks like this:
[karim@srv-boutique ~]$ ← Rocky / RHEL
karim@srv-boutique:~$ ← Debian / Ubuntu
root@srv-boutique:/etc# ← root, in the /etc directory
Let's break it down:
| Part | Meaning |
|---|---|
karim |
the logged-in user |
srv-boutique |
the name of the machine (hostname) |
~ |
the current directory; ~ means "my home directory" |
$ |
you are a normal user |
# |
you are root: everything is allowed, be careful! |
In documentation (and in this course), a line starting with
$is typed as a normal user, and a line starting with#is typed as root. Do not copy the$or#itself.
The prompt format is set by the PS1 variable. You can look at it without changing it:
echo "$PS1" # for example: [\u@\h \W]\$ (\u = user, \h = machine, \W = directory)
3. Your first commands
These commands change nothing: you can type them without worry.
whoami # who am I? → karim
hostname # on which machine? → srv-boutique
date # date and time
uptime # how long the machine has been running, and its load
uname -r # the Linux kernel version
cal # a small calendar (not always installed)
clear # clears the screen (or Ctrl+L)
4. The shape of a command
A command has three parts separated by spaces:
ls -l /etc
#│ │ └── argument: WHAT to act on (here the /etc directory)
#│ └─────── option: HOW to act (here "long format")
#└──────────── the command itself
A few rules to know from day one:
- Short options start with a dash:
-l,-a. You can group them:ls -la=ls -l -a. - Long options start with two dashes:
--all,--help. They are easier to read in a script. - A name containing a space must be in quotes:
ls "Mes documents". Without quotes, the shell sees two arguments,Mesanddocuments. - Upper and lower case matter:
Rapport.txtandrapport.txtare two different files.
5. Built-in commands and programs
Some commands are programs stored on the disk (/usr/bin/ls). Others are built into the shell (builtin), such as cd, echo or type. The type command tells you what a name is:
type ls # ls is aliased to `ls --color=auto' (an alias, see below)
type cd # cd is a shell builtin (built into the shell)
type date # date is /usr/bin/date (a program)
To find out where a program is located, use command -v date or which date (the latter is not always installed).
6. Saving time at the keyboard
An administrator types a lot. These bash shortcuts work on every distribution:
| Key | Effect |
|---|---|
| Tab | completes a command or file name; Tab twice = list of possibilities |
| ↑ / ↓ | moves up / down through the commands you already typed |
| Ctrl+R | searches the history (type part of a command) |
| Ctrl+C | interrupts the running command |
| Ctrl+L | clears the screen |
| Ctrl+A / Ctrl+E | start / end of the line |
| Ctrl+D | on an empty line: closes the shell (like exit) |
history # the numbered list of your latest commands
!42 # runs command number 42 from the history again
!! # runs the last command again
The history is saved in the file ~/.bash_history when you exit the shell.
7. Aliases: your own shortcuts
An alias replaces a word with a longer command. Example: a shop manager often checks the most recent files in the sales directory.
alias lt='ls -lt' # lt will show files from newest to oldest
lt # you use it like a command
alias # lists all defined aliases
unalias lt # removes the alias
An alias typed this way disappears when you close the shell. To keep it, you write it in the file ~/.bashrc, which bash reads every time it starts:
echo "alias lt='ls -lt'" >> ~/.bashrc # >> appends to the end of the file
source ~/.bashrc # reloads the file right away
On Rocky / RHEL, root's
.bashrcalready contains safety aliases such asalias rm='rm -i'(asks for confirmation before deleting). This is not the case on Debian and Ubuntu: the same command deletes without asking.
8. Root, the all-powerful user
The root account can do anything, including destroying the system with a single command. On a real server, you work as a normal user and become root only for administration tasks (with sudo, which you will see in the distribution tracks). In the MasterLinux labs, you are root to keep things simple: it is a sandbox that will be erased, so you risk nothing.
Common pitfalls
- Copying the
$or#from the examples: the shell answerscommand not found. - Forgetting the space between the command and the option:
ls-ldoes not exist, it isls -l. - Typing in upper case (
Ls,CD): Linux does not recognise the command. - Pasting a command found on the Internet without understanding it, especially as root. Read it first and check every word.
Lab
Your Rocky Linux terminal is waiting for you, as root (prompt ending with #). You will:
- ask the machine who you are and keep the answer in a file;
- do the same with the date;
- ask the shell whether
cdis a program or a built-in command; - create a permanent
ltalias in your.bashrc.
The > sign sends a command's output into a file (it replaces the file), and >> appends to the end. You will study them in detail in lesson 1.8. Click Check after each task.
Quiz
- What is the difference between the terminal and the shell?
- What does the
#at the end of the promptroot@web01:/var/log#mean? Which directory are you in? - Does the command
LS -Lwork? Why? - How do you make an alias permanent?
- Which key automatically completes a file name?
Answers
- The terminal is the window that displays and receives text; the shell (bash) is the program that interprets commands.
- You are root (administrator); the current directory is
/var/log. - No: Linux is case-sensitive. You must type
ls -l. - By writing it in
~/.bashrc(thensource ~/.bashrcor a new shell). - The Tab key.
Reference sources: Debian Reference (free licence), Ubuntu Server Guide (CC BY-SA 3.0), Red Hat Enterprise Linux documentation (CC BY-SA 3.0). Original MasterLinux text.