The shell and the command line
When you login to a Unix machine you are confronted with a prompt: a short piece of text indicating that the system is ready for you to type a command. The prompt on Snowy (a computer at Melbourne Bioinformatics) might look like this:
where the word
username is replaced by your actual username on the system. The content of the prompt is configurable, and it might be different on other systems that you encounter. In this document, for simplicity, we show the prompt as a single dollar sign (
The command line itself is provided to you by a program called the shell. The shell acts as an intermediary between you and the underlying operating system. Each time you enter a command, the shell parses it and then asks the system to carry it out. The shell is also programmable, which means you can automate sequences of commands that would be tedious to enter manually. Most Unix systems provide several alternative shells for you to use, but most GNU/Linux systems use the bash shell as the default. This document assumes you are using bash, but most of the examples should work equally well in other shells.
The diagram below shows how the shell relates to various other components in a computer system:
At the bottom of the diagram sits the computer hardware. Immediately above the hardware is a software abstraction called an operating system kernel. The kernel is a piece of software which manages the resources of the computer (disk drives, screen, keyboard etc) and makes them available to your programs. User applications sit on top of the stack and interface with the hardware via the kernel. The shell is just an ordinary user application that happens to provide a convenient command interpreter.
It should be noted that users can interact with Unix in other ways besides the shell. Graphical user interfaces (GUIs) are also available, and most users run a shell inside a GUI window nowadays. Nonetheless, this document focuses on the shell because its programmability offers productivity gains unrivalled by other interfaces.
An example command
Here’s a simple example command to get you started. Type the following command into the shell and press enter:
the output will look something like this:
Tue Nov 24 15:11:58 AEDT 2015
As you can see the output of the command is the current local date and time.
Note that once the previous command has completed the shell displays a new prompt indicating that it is ready for the next command to be entered.
Many Unix commands accept arguments (sometimes called flags) which request specific features to be activated. For example you can ask the
date command to produce its output in Coordinated Universal Time (UTC) like so:
$ date -u Tue Nov 24 15:12:14 AEDT 2015
It is not uncommon for a command to accept many different arguments, and, in most cases, more than one argument can be supplied at the same time. Arguments are separated by one or more space characters and they are usually case-sensitive.
How do you find out what arguments are accepted by a command? In the case of
date you can pass it the
--help argument and it will print out some usage information, but unfortunately not all commands behave the same way. We cover the question of how to get help when using a Unix system in the Getting Help section later in this document.
A convenient feature of the shell is that it keeps a record of the most recent commands that you have entered. It is often the case that previous commands are repeated in the near future. You can go back to previous commands by using the up-arrow key (and forwards with the down-arrow key). The command history saves you from typing it all in again each time, which is very useful when the command was large or complex.
You can also view your most recent commands with
$ history ... 980 ls -la 981 ./myprog 982 man awk
(Your output will look different to the example above, because you will have a different history of commands.)
Each command in the history is numbered and the shell lets you run the command again by prefixing its number with an exclamation mark. For example, given the history above, the command
!980 would cause
ls -la to be run again.
The command line can be edited in a number of ways:
Forward and back arrows move the cursor left and right.
The delete key erases the character to the left of the cursor.
Control-k deletes everything from the cursor to the end of the line.
Control-u deletes everything from the cursor to the start of the line.
Control-y inserts text deleted with the previous Control-k or Control-u at the current cursor position
Control-e moves the cursor to the end of the line.
Control-a moves the cursor to the start of the line.
Control-r searches in reverse through the history.
Pressing the tab key causes the shell to expand the text sitting just prior to the cursor:
If zero names match then nothing happens.
If exactly one file name matches then the text is expanded to that name.
If more than one file name matches, then the text is expanded to show the characters that are common to all possible matches. For example, if your current directory has files called "myfile", "myfile.1", "myfile2" and "myfileX", and you type:
ls -l my<tab>
Then your command will be expanded to
ls -l myfile
- If your command is already expanded to cover the maximum number of characters that match all files you can then press tab a second time to display a list of matching filenames. This enables you to type one or more characters to disambiguate before pressing tab to continue the completion.