Terminal and shell: command structure and getting help
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- The terminal is the window you type in; the shell is the program that runs the commands. On Ubuntu the default shell is bash.
- A command has three parts: the command, options and arguments:
ls -l /etc. - A short option uses one dash (
-l), a long option uses two (--all). Short options can be combined:-la. - Linux is case-sensitive:
lsworks butLSdoes not;File.txtandfile.txtare two different files. - For help:
man command(full manual),command --help(short list),whatis command(one-line summary). Tab completes names, ↑ brings back earlier commands.
Terminal versus shell
When you connect to a server over SSH you get a black window. That window is the terminal: it only shows text and takes keyboard input.
The program that understands and runs what you type is the shell. On Ubuntu the default shell is bash (Bourne Again SHell). You can check which shell you are in like this:
ali@srv-web01:~$ echo $SHELL /bin/bash
$SHELL is a variable: the system keeps the path of your shell in it. echo prints the text it is given.
The structure of a command
Almost every command follows the same pattern:
| Part | Example | What it does |
|---|---|---|
| Command | ls | What to do (show a list) |
| Option | -l | How to do it (in detail) |
| Argument | /etc | What to do it on (which directory) |
ali@srv-web01:~$ ls -l /etc/hostname -rw-r--r-- 1 root root 10 Dec 1 10:00 /etc/hostname
Options come in two forms:
- Short: one dash and one letter:
-l,-a,-h. Several can be combined:ls -l -a -his the same asls -lah. - Long: two dashes and a word:
--all,--human-readable. The long form is easier to read and is common in scripts.
Watch the letters: Linux is case-sensitive. ls -l and ls -L are different options, and Readme.txt and readme.txt are two separate files.
Saving time: Tab and history
- Tab: type the start of a command or file name and press Tab — the shell completes the rest. Press it twice to list the matching options.
- ↑ and ↓: bring back commands you typed before, so you do not retype them.
- Ctrl+C: stops a running command (for example a
pingthat never ends). - Ctrl+L or
clear: clears the screen.
Tab is not only convenient: completing a path with Tab leaves no room for typos.
Getting help
You do not need to remember every command. On Linux the help lives inside the terminal.
ali@srv-web01:~$ whatis ls ls (1) - list directory contents ali@srv-web01:~$ ls --help | head -n 4 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.
| Way | What it gives | When |
|---|---|---|
whatis ls | A one-line description | To remember what a command does |
ls --help | A short list of options | To find the option you need quickly |
man ls | The full manual | To learn in detail |
apropos copy | Searches commands by keyword | When you do not know the command name |
Moving around a man page: space — next page, /word — search, n — next match, q — quit.
Where is a command?
Sometimes you need to know what a command actually is:
ali@srv-web01:~$ type cd cd is a shell builtin ali@srv-web01:~$ which ls /usr/bin/ls
cd is built into the shell, while ls is a separate program in /usr/bin. The shell looks for the command you type in the directories listed in the PATH variable.
You can try these commands in the Linux simulator: type help, then man ls.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What is the default shell on Ubuntu?
echo $SHELL → /bin/bash.2In ls -l /etc, what is /etc?
ls is the command, -l is an option and /etc is the argument it works on.3Which command is the same as ls -lah?
4How do you open the full manual of a command?
man opens the manual; press q to quit.5Which is true about File.txt and file.txt?
Comments
Sign in to comment.