Linux Command-Line Fundamentals
Exit

Lesson 1 of 18 · User

The terminal, the shell and the prompt

20 min Rocky Linux 9

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: ls works, LS does 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, Mes and documents.
  • Upper and lower case matter: Rapport.txt and rapport.txt are 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 .bashrc already contains safety aliases such as alias 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 answers command not found.
  • Forgetting the space between the command and the option: ls-l does not exist, it is ls -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:

  1. ask the machine who you are and keep the answer in a file;
  2. do the same with the date;
  3. ask the shell whether cd is a program or a built-in command;
  4. create a permanent lt alias 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

  1. What is the difference between the terminal and the shell?
  2. What does the # at the end of the prompt root@web01:/var/log# mean? Which directory are you in?
  3. Does the command LS -L work? Why?
  4. How do you make an alias permanent?
  5. Which key automatically completes a file name?
Answers
  1. The terminal is the window that displays and receives text; the shell (bash) is the program that interprets commands.
  2. You are root (administrator); the current directory is /var/log.
  3. No: Linux is case-sensitive. You must type ls -l.
  4. By writing it in ~/.bashrc (then source ~/.bashrc or a new shell).
  5. 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.

Practical exercises

To do on Rocky Linux 9

The online terminal is coming soon. Meanwhile, do these tasks on your own Linux machine, a virtual machine or WSL.

  1. Save the name of the logged-in user in /root/moi.txt (whoami command).
    Show a hintThe > sign sends a command's output into a file: whoami > /root/moi.txt
    Show the solution
    whoami > /root/moi.txt

    Check : grep -qx root /root/moi.txt

  2. Save the current date and time in /root/date.txt (date command).
    Show a hintSame principle: date, then the > sign, then the file name.
    Show the solution
    date > /root/date.txt

    Check : test -s /root/date.txt

  3. Ask the shell what the cd command is (type command) and save the answer in /root/type-cd.txt.
    Show a hinttype cd > /root/type-cd.txt — the answer tells you whether it is a program or a command built into the shell.
    Show the solution
    type cd > /root/type-cd.txt

    Check : grep -qi -e builtin -e intégrée /root/type-cd.txt

  4. Create a permanent shortcut: add the line alias lt='ls -lt' at the end of the file /root/.bashrc.
    Show a hintUse >> (two angle brackets) to APPEND at the end: echo "alias lt='ls -lt'" >> /root/.bashrc
    Show the solution
    echo "alias lt='ls -lt'" >> /root/.bashrc

    Check : grep -q "^alias lt='ls -lt'" /root/.bashrc

Official references consulted: Debian Reference — chapitre 1, tutoriels GNU/Linux ; Ubuntu Server Guide — prise en main de la ligne de commande. Original MasterLinux text.