Skip to content

Shell basics ​

By the end of this lesson you can read a prompt, type a command with options and arguments, move between folders with absolute and relative paths, and get help when a command is new to you.

Terminal, shell and command ​

Three words get mixed up all the time. They are three different things:

text
┌─ Terminal ──────────────────────────────────────────┐
│  the window: shows text, sends your keystrokes      │
│                                                     │
│   ┌─ Shell (bash) ───────────────────────────────┐  │
│   │  reads the line you type, finds the program, │  │
│   │  starts it, shows you a new prompt           │  │
│   │                                              │  │
│   │   sam@laptop:~$ ls Documents                 │  │
│   │                 └┬┘                          │  │
│   │              Command: a program (ls)         │  │
│   └──────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────┘
  • The terminal is the window. It shows text and passes on what you type. GNOME Terminal, Konsole, Ubuntu Terminal and the terminal inside SecureShell Go are all terminals.
  • The shell is the program running inside it. It reads the line you type, works out what you mean, and starts the right program. On most Linux systems the shell is Bash. macOS uses zsh, which behaves almost the same for everything in this track.
  • A command is what you ask for. Most commands are small programs, such as ls, which lists files. A few, such as cd, are built into the shell itself.

People often say "the command line" for all three together.

Read the prompt ​

When the shell is ready for you, it shows a prompt. On Ubuntu it looks like this:

text
sam@laptop:~$
PartMeaning
samThe user you are signed in as
laptopThe name of the computer. Useful when you are connected to a remote one
~The folder you are in. ~ is short for your home folder
$Ready for a command. A # here means you are the root user, who can change anything: be careful

You type after the $ and press Enter. The shell runs the command, prints its output, and shows a new prompt. In this track, when an example shows both the command and its output, the command line starts with the prompt and the output follows without one.

Commands, options and arguments ​

A command line is a list of words separated by spaces:

text
ls  -l  -a  Documents
│   └──┬──┘ └───┬───┘
│   options  argument
command
  • The command comes first: which program to run.
  • Options (also called flags) change how it behaves. Short options are a dash and one letter, such as -l (long format). You can join short options: ls -la is the same as ls -l -a. Long options are two dashes and a word, such as --all.
  • Arguments say what to work on: here, the Documents folder. Without an argument, ls lists the folder you are in.

Spaces separate words, so a name with a space in it must be quoted: ls "My Photos". Upper and lower case matter: Documents and documents are different names, and LS is not a command.

Where am I? pwd, ls and cd ​

Three commands get you around. Try them now:

text
sam@laptop:~$ pwd
/home/sam
sam@laptop:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
sam@laptop:~$ cd Documents
sam@laptop:~/Documents$ pwd
/home/sam/Documents
CommandStands forWhat it does
pwdprint working directoryShows the folder you are in
lslistLists the files and folders in it
cdchange directoryMoves you to another folder

Directory is the Linux word for folder; the two mean the same thing. The folder you are in is your working directory. Notice how the prompt changed from ~ to ~/Documents after cd.

ls has two options you will use every day. Here they are in a practice folder that holds two folders and one empty file (you will make folders like this in the next lesson):

text
sam@laptop:~/practice$ ls -l
total 8
drwxrwxr-x 2 sam sam 4096 Nov  9 17:43 notes
drwxrwxr-x 2 sam sam 4096 Nov  9 17:43 photos
-rw-rw-r-- 1 sam sam    0 Nov  9 17:43 todo.txt
sam@laptop:~/practice$ ls -a
.  ..  notes  photos  todo.txt

-l shows details: lines starting with d are folders, lines starting with - are files. You will decode the rest in Permissions. -a shows hidden files too: names that start with a dot, such as .bashrc, are hidden from a plain ls.

Paths: absolute and relative ​

A path is the address of a file or folder. Linux has one tree of folders, starting at /, the root folder:

text
/                      the root: the top of everything
├── etc/               system settings
├── home/
│   └── sam/           your home folder, also called ~
│       ├── Documents/
│       │   └── cv.pdf
│       └── Downloads/
├── tmp/               temporary files
└── usr/               installed programs

There are two ways to write a path:

  • An absolute path starts with / and works from anywhere: /home/sam/Documents/cv.pdf.
  • A relative path does not start with /. It is read from the folder you are in. From /home/sam, the same file is Documents/cv.pdf.

Some short names help:

NameMeansExample
~Your home foldercd ~/Downloads
.The folder you are inls . is the same as ls
..The folder above this onecd .. goes up one level
- (with cd)The folder you were in beforecd - jumps back

You can chain them. From /home/sam/Documents, cd ../Downloads goes up to /home/sam and then into Downloads. cd on its own always takes you home.

If you get the name wrong, the shell tells you:

text
sam@laptop:~$ cd Documnets
bash: cd: Documnets: No such file or directory

Type less: tab completion and history ​

You rarely have to type a whole name:

  • Tab completion. Type the first few letters and press Tab. cd Doc then Tab becomes cd Documents/. If several names match, press Tab twice to list them. It also completes command names. As a bonus, it never makes a typing mistake.
  • History. Press ↑ to bring back the previous command, and again for the one before. Edit it with ← and →, then press Enter.
  • Search history. Press Ctrl + R and type part of an old command. Press Ctrl + R again for older matches, Enter to run it.
  • history lists your recent commands with numbers.

Get help: --help and man ​

Nobody remembers every option. Two ways to look them up:

  • Add --help to most commands for a short summary: ls --help. It starts with a usage line like Usage: ls [OPTION]... [FILE]..., where square brackets mean "optional" and ... means "you can give more than one".
  • man ls opens the full manual page. Scroll with the arrow keys or Space, search with / and a word, and press q to quit.

Built-in shell commands such as cd are described by help cd instead.

Stop, clear and leave ​

Keys or commandWhat it does
Ctrl + CStops the command that is running. Your first move when something hangs or floods the screen
Ctrl + L or clearClears the screen. History is kept
qQuits a full-screen program such as man, less or top
exit or Ctrl + DCloses the shell, or ends a remote session

Because Ctrl + C means "stop", most Linux terminals copy with Ctrl + Shift + C and paste with Ctrl + Shift + V.

Stay safe ​

  • Read before you press Enter, especially a command you copied from a web page or a chat. A pasted line runs with all your rights. If you do not understand a command, look it up first.
  • Check where you are. Look at the prompt, or run pwd, before a command that changes or deletes files. The same command does different things in different folders.
  • Notice # in the prompt. It means you are root, and nothing will stop a mistake. Lesson 3 explains how to avoid working as root.
  • Remote is real. On a server, connected with SecureShell Go or ssh, every command runs on that machine. The host name in the prompt tells you which machine you are on.

Try it in a Dhiva Labs app ​

  • On Windows, Ubuntu Terminal gives you an Ubuntu-style prompt with pwd, ls and cd, and DhivaLabs Shell gives you Bash with tab completion, history and Ctrl + R.
  • To practise on a remote Linux machine you own, connect with SecureShell Go. Its extra-keys row has Tab, Ctrl and ^C for a phone keyboard.

Check yourself ​

QuizShell basics

Question 1 of 6

Which one is the shell?

Score: 0

What you learned ​

  • The terminal is the window, the shell reads your commands, and a command is usually a small program.
  • A command line is a command, then options that change it, then arguments that say what to work on.
  • pwd shows where you are, ls lists, cd moves. Absolute paths start with /; relative paths start from where you are.
  • Tab completes, ↑ and Ctrl + R recall, --help and man explain, Ctrl + C stops.

Next steps ​