Anatomy of a Unix Command

A basic UNIX command has 3 parts. These are:

  1. command name

  2. options (zero or more)

  3. arguments (zero or more)

In general, UNIX commands are written as single strings followed by the return key. Note that UNIX commands are case-sensitive, i.e. it does matter whether you type a letter in uppercase or lowercase.

Example

wc –l myfile.txt

The example above has the following components:

  • the command name (wc)

  • one option (-l)

  • one argument (myfile.txt)

Options

  • Also called “switches” or “flags”.

  • Specify the way a given commands must work.

  • Preceded by one or two dashes (one dash if the switch is a single character, two dashes otherwise).

Tab completion

Tab completion is a useful features of UNIX where the shell automatically fills in partially typed commands when the tab key is used. This is advantageous in many ways:

  • Commands or filenames with long or difficult spelling require fewer keystroke to reach

  • In the case of multiple possible completions, the shell will list all filenames beginning with those few characters.

History

Another useful feature is the fact that the shell remembers each typed command, so you can easily recall and run previous commands.

  • By using the up and down arrows at the prompt, you can revisit the command history and recall previously typed commands.

  • Previous commands can be re-executed as they are or can be edited using the left/right arrow.

Use the history command to specify how many and which previous commands to display.

# display last typed commands
history 
#display the last n typed commands
history n
#execute command n
!n
#execute last command in the list
!!
# recall and execute the n-th last command
!-n

Examples:

# recall the last 5 typed commands
history 5 
# recall and execute the 3rd last command
!-3
# recall and execute command number 120
!120

Last updated

Massachusetts Institute of Technology