Skip to content
elephantoo

The terminal & shell basics

Lesson 2 of 31 14 min read

The prompt, command syntax, options and arguments, man pages, history, tab completion and shortcuts.


The command line can look intimidating: a blinking cursor and no buttons. In fact it's a conversation. You type a command, the computer replies. In this lesson you'll learn how that conversation works, how commands are structured, how to get help for any command, and the shortcuts that make you fast.

Terminal vs shell#

  • The terminal (terminal emulator) is the window: GNOME Terminal, Konsole, Windows Terminal, iTerm2. It shows text and sends your keystrokes on.
  • The shell is the program running inside it that interprets your commands. Most Linux systems default to bash (the Bourne Again SHell). macOS defaults to zsh, which is very similar. Other shells include fish and dash.
Terminal
echo $SHELL     # your login shell
echo $0         # the shell you're running right now
Output
/bin/bash
bash

The prompt#

When the shell is ready, it shows a prompt:

Output
ada@laptop:~/projects$

That's user@host:current directory, followed by $ for a normal user or # for root. ~ is shorthand for your home directory. In this course, commands are shown without the prompt so you can copy and paste them.

Anatomy of a command#

Output
ls   -l -h   --sort=time   /var/log
│    │        │             └─ argument: what to act on
│    │        └─ long option (with a value)
│    └─ short options
└─ command
  • Command: the program to run (ls, cp, git) or a shell built-in (cd, echo).
  • Options (flags) change behaviour. Short options use one dash and one letter (-l) and can be combined (-lh = -l -h). Long options use two dashes and words (--human-readable).
  • Arguments are what the command works on: files, directories, URLs, text.
  • Words are separated by spaces. To pass something containing spaces as one argument, quote it: mkdir "My Projects".

Linux is case-sensitive: ls, LS and Ls are different, and so are notes.txt and Notes.txt.

Getting help#

You never need to memorise every option. Every command documents itself:

Terminal
ls --help | head -5
Output
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  • man ls opens the full manual page. Scroll with the arrow keys or Space, search with /word (then n for the next match), and quit with q.
  • man -k keyword (or apropos keyword) searches all manual pages, e.g. man -k compress.
  • help cd shows help for shell built-ins, which don't have their own man page.
  • tldr tar (install the tldr package) shows short, practical examples. It's a great complement to man pages.

Is a command a program, a built-in or an alias? type tells you:

Terminal
type cd
type ls
type -a echo
Output
cd is a shell builtin
ls is /usr/bin/ls
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

Your output for ls may say it's an alias (for ls --color=auto) on Ubuntu.

Running several commands#

Terminal
mkdir demo; cd demo            # ; runs commands one after another
mkdir -p a/b && echo "created"  # && runs the second only if the first succeeded
cd missing-dir || echo "no such directory, carrying on"   # || runs the second only if the first failed
Output
created
bash: cd: missing-dir: No such file or directory
no such directory, carrying on

Every command finishes with an exit status: 0 means success and anything else means failure. $? holds the last one:

Terminal
true;  echo "status: $?"
false; echo "status: $?"
ls /nope 2>/dev/null; echo "status: $?"
Output
status: 0
status: 1
status: 2

You'll use exit statuses constantly in scripts.

History: never retype a command#

The shell remembers what you've typed (in ~/.bash_history):

Keys / commandWhat it does
↑ / ↓previous / next command
Ctrl+R then typesearch history backwards; Ctrl+R again for older matches, Enter to run, → to edit
historylist recent commands with numbers
!42rerun command number 42
!!rerun the last command, e.g. sudo !! when you forgot sudo
!$the last argument of the previous command: mkdir proj && cd !$

Tab completion#

Press Tab to complete command names, file names, directories and, with bash-completion installed, even options and git branches. If several completions match, press Tab twice to list them. Tab completion saves typing and prevents typos: if it doesn't complete, the file probably isn't there.

Keyboard shortcuts worth learning#

ShortcutAction
Ctrl+Ccancel the running command (or the line you're typing)
Ctrl+Dend of input; on an empty line it logs out of the shell
Ctrl+Lclear the screen (same as clear)
Ctrl+A / Ctrl+Ejump to the start / end of the line
Ctrl+U / Ctrl+Kdelete to the start / end of the line
Ctrl+Wdelete the previous word
Alt+.insert the last argument of the previous command
Ctrl+Zsuspend the running program (resume with fg; see Processes)

Copy and paste in most Linux terminals use Ctrl+Shift+C and Ctrl+Shift+V, because Ctrl+C means "cancel".

A quick tour of useful everyday commands#

Terminal
echo "Hello, $USER"        # print text (variables are expanded)
date +%Y-%m-%d             # today's date in a chosen format
cal                        # a calendar (package: ncal on Debian/Ubuntu)
whoami; id                 # your user and groups
clear                      # clear the screen
exit                       # close the shell

Common mistakes#

  • Spaces in the wrong place. cd/etc isn't cd /etc, and rm -rf / tmp/x (with a stray space) is very different from rm -rf /tmp/x. Read before you press Enter.
  • Unquoted names with spaces. cd My Documents passes two arguments. Use cd "My Documents" or Tab completion.
  • Pasting commands you don't understand from the internet, especially with sudo or curl ... | bash. Read them first.
  • Panicking in a program. Most programs quit with q (man, less, top) or Ctrl+C. In vim, it's Esc then :q!. You'll learn more in Text editors.

What's next#

Now that you can talk to the shell, let's look at the map: how the Linux filesystem is organised.

Check your understanding

Quick quiz

0/3 answered
  1. 1.What's the difference between the terminal and the shell?

  2. 2.In ls -l /etc, what are -l and /etc?

  3. 3.Which key combination searches backwards through your command history?

Finished reading?

Mark this lesson complete to track your progress.