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:
┌─ 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 ascd, 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:
sam@laptop:~$| Part | Meaning |
|---|---|
sam | The user you are signed in as |
laptop | The 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:
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 -lais the same asls -l -a. Long options are two dashes and a word, such as--all. - Arguments say what to work on: here, the
Documentsfolder. Without an argument,lslists 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:
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| Command | Stands for | What it does |
|---|---|---|
pwd | print working directory | Shows the folder you are in |
ls | list | Lists the files and folders in it |
cd | change directory | Moves 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):
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:
/ 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 programsThere 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 isDocuments/cv.pdf.
Some short names help:
| Name | Means | Example |
|---|---|---|
~ | Your home folder | cd ~/Downloads |
. | The folder you are in | ls . is the same as ls |
.. | The folder above this one | cd .. goes up one level |
- (with cd) | The folder you were in before | cd - 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:
sam@laptop:~$ cd Documnets
bash: cd: Documnets: No such file or directoryType less: tab completion and history
You rarely have to type a whole name:
- Tab completion. Type the first few letters and press Tab.
cd Docthen Tab becomescd 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.
historylists your recent commands with numbers.
Get help: --help and man
Nobody remembers every option. Two ways to look them up:
- Add
--helpto most commands for a short summary:ls --help. It starts with a usage line likeUsage: ls [OPTION]... [FILE]..., where square brackets mean "optional" and...means "you can give more than one". man lsopens 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 command | What it does |
|---|---|
| Ctrl + C | Stops the command that is running. Your first move when something hangs or floods the screen |
Ctrl + L or clear | Clears the screen. History is kept |
| q | Quits a full-screen program such as man, less or top |
exit or Ctrl + D | Closes 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 areroot, and nothing will stop a mistake. Lesson 3 explains how to avoid working asroot. - 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,lsandcd, 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
Question 1 of 6
Which one is the shell?
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.
pwdshows where you are,lslists,cdmoves. Absolute paths start with/; relative paths start from where you are.- Tab completes, ↑ and Ctrl + R recall,
--helpandmanexplain, Ctrl + C stops.