The terminal & shell basics
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.
The prompt#
When the shell is ready, it shows a prompt:
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#
- 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:
man lsopens the full manual page. Scroll with the arrow keys or Space, search with/word(thennfor the next match), and quit withq.man -k keyword(orapropos keyword) searches all manual pages, e.g.man -k compress.help cdshows help for shell built-ins, which don't have their own man page.tldr tar(install thetldrpackage) 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:
Your output for ls may say it's an alias (for ls --color=auto) on Ubuntu.
Running several commands#
Every command finishes with an exit status: 0 means success and anything else means failure. $? holds the last one:
You'll use exit statuses constantly in scripts.
History: never retype a command#
The shell remembers what you've typed (in ~/.bash_history):
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#
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#
Common mistakes#
- Spaces in the wrong place.
cd/etcisn'tcd /etc, andrm -rf / tmp/x(with a stray space) is very different fromrm -rf /tmp/x. Read before you press Enter. - Unquoted names with spaces.
cd My Documentspasses two arguments. Usecd "My Documents"or Tab completion. - Pasting commands you don't understand from the internet, especially with
sudoorcurl ... | bash. Read them first. - Panicking in a program. Most programs quit with
q(man, less, top) orCtrl+C. In vim, it'sEscthen: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
1.What's the difference between the terminal and the shell?
2.In
ls -l /etc, what are-land/etc?3.Which key combination searches backwards through your command history?
Finished reading?
Mark this lesson complete to track your progress.