4. Commands

$Id: commands.xml 2330 2009-04-17 15:45:52Z rafi $

Table of Contents

4.1. Piping
4.2. Man Pages
4.3. Pagers

You're always using commands when dealing with a terminal. You need to type them in at the shell prompt in order to do something. The shell then executes the command and shows you that it is done with it when displaying the prompt again. In order to execute the command, which by the way is nothing more than a program, the shell needs to know where to search for commands. This is told the shell using the PATH environment variable. It holds the paths to search for commands by the shell. To see the contents of this variable, type in the command as shown by the example Example 5, “The PATH environment variable”.

Example 5. The PATH environment variable

The echo command can be used to print text and variables to the screen. To see the value of the PATH environment variable, type the command as shown below. Unfortunately, it is beyond the topic of this article to fully explain what environment variables are and how to deal with them.

$ echo $PATH

As you can see, the paths are separated by colons.

If you type in a command in the shell, the shell searches for the command in the directories /sbin, /bin, /usr/sbin, and /usr/bin in exactly this order, given the PATH environment variable is set according to the example above. It starts the command first found. So, if you happen to have a command named foo in /sbin and /bin it will always start the one found in /sbin because of the order given.

Suppose you want to execute a command in a directory other than the ones specified in the PATH environment variable, you have to enter the full file name

Example 6. Executing a command not in your PATH environment variable

$ /home/joe/foo/bar

You mostly won't start commands by simply using their name on the command line, but you want to tell them how they should behave. You do this by supplying command line arguments, which are mostly referred to as options or switches. Every command takes different arguments, but the way you specify them is most of the time the same, as shown in the example Example 7, “Command line arguments”

Example 7. Command line arguments

$ somecommand [arg1] [arg2] ... [argN]

As you see by the example, you can specify more than one argument, but the key point is that they are separated by spaces. Another important thing to note is, that almost every command line argument starts with a dash (-) if it consists of only one letter, or by two dashes (--) if the option is a term. Those are called long options. However, that is a rule of thumb, and the programmer of the command is free to expect the options in their own way. Therefore, each well programmed command has an option that will display a help text to the user, which is mostly invoked using either -h, or --help, as shown in the example Example 8, “The help switch”.

Example 8. The help switch

$ yapet -h

yapet [-c] [-h] [-V] [<filename>]

-c, --copyright show copyright information

-h, --help      show this help text

-V, --version   show the version of YAPET

<filename>      open the specified file <filename>

YAPET stores passwords encrypted on disk using the blowfish algorithm.
The encryption key is computed from the master password using md5, sha1, and
ripemd-160 digest algorithms producing a 448 bit (56 bytes) key.

Most commands allow you to group single letter options together. As an example, I'll use the ls command, which is explained in more depth in Section 5.2, “The ls Command”

Example 9. Grouping command options

You can give each options separated with a space

$ ls -l -a -h
total 84K
drwxr-xr-x  2 joe joe   42 2009-01-03 00:34 .
drwx------ 80 joe joe 8.0K 2009-01-03 00:33 ..
-rw-r--r--  1 joe joe   40 2009-01-03 00:34 file1
-rw-r--r--  1 joe joe  123 2009-01-03 00:34 file2
-rw-r--r--  1 joe joe  62K 2009-01-03 00:35 file3

or you might group them together, as such

$ ls -lah
total 84K
drwxr-xr-x  2 joe joe   42 2009-01-03 00:34 .
drwx------ 80 joe joe 8.0K 2009-01-03 00:33 ..
-rw-r--r--  1 joe joe   40 2009-01-03 00:34 file1
-rw-r--r--  1 joe joe  123 2009-01-03 00:34 file2
-rw-r--r--  1 joe joe  62K 2009-01-03 00:35 file3

which produces the same result.

Sometimes you want to know where the command you want to start is located. UNIX provides the command which for this purpose. It displays the full path where the command is located. Which uses the PATH environment variable to locate the command in question. See the example below.

Example 10. The which command

$ which gtar

If the command is not found, which prints an error message

Example 11. Error message of which

$ which dunno
dunno: Command not found.

4.1. Piping

UNIX allows you to feed the output of one command to another command for further processing. This is called piping. The output is redirected to another program by concatenating the two commands using the pipe symbol “|”. The next example should make this more clear.

Example 12. Redirecting the output using a pipe

Suppose you have a file containing an unordered list of names. You want to find all names containing a certain sequence of characters and print out all those names in a sorted fashion. You would need two commands to accomplish this, namely grep which searches for character strings in a file and sort which sorts the input it receives. Assuming your list is called names.txt. Start first with grepping the file for the sequence “in”, you would do

$ grep 'in' names.txt

which produces the output that is obviously unsorted


if you do it again, but piping the output to sort, it looks more the way we want it

$ grep 'in' names.txt | sort

You can pipe to any command that reads from the standard input. Refer to the man page (see Section 4.2, “Man Pages”) of the command in question to find out whether or not it supports piping. For example, the man page of sort states that


     /usr/bin/sort [-bcdfimMnru] [-k keydef] [-o output]
	 [-S kmem] [-t char] [-T directory] [-y [kmem]]
	 [-z recsz] [+pos1 [-pos2]] [file]...


     The following operand is supported:

     file    A path name of  a  file  to  be  sorted,  merged  or
	     checked.  If no file operands are specified, or if a
	     file operand is -, the standard input is used.

The standard input is where the command reads the keystrokes when you're typing on the keyboard. It's counterpart is the standard output. The output of commands you see in a terminal is printed to standard output. The standard input is for reading and the standard output is for writing by the program.

You might wonder why they are called standard input and output. Well, if the program does not setup things differently, they are the default input and output “channels”. A program might read from a file, which is not a standard input. Same applies when writing to a file, which is not standard output..

4.2. Man Pages

UNIX provides a help system called “Man Pages”, which is short for “Manual Pages”. Most commands come along with a man page. You simply need to type in man and provide the command you want to know more about as argument as shown in Example 13, “Example of a man page” below.

Example 13. Example of a man page

$ man ls
LS(1)                   FreeBSD General Commands Manual                LS(1)

     ls -- list directory contents

     ls [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuwx1] [file ...]

     For each operand that names a file of a type other than directory, ls
     displays its name as well as any requested, associated information.  For
     each operand that names a file of type directory, ls displays the names
     of files contained within that directory, as well as any requested, asso-
     ciated information.

Man pages are divided in sections. If you don't specify the section when invoking the man command, man searches all the sections and picks the first manual page it finds. In fact, it is possible for commands to appear in more than one sections, as shown in the example below. The example uses the whatis command that displays a one-line summary about commands.

Example 14.  Example of a command that appears in more than one man page section

$ whatis tr
tr              tr (1)          - translate characters
tr              tr (1b)         - translate characters

As you see by the example above, the tr command is listed in the section 1 and 1b (the sections are shown in parentheses). The Table 1, “The standard man page sections” lists the basic man page sections. To display a man page from a particular section, you enter the man command in the following manner under FreeBSD and Linux

$ man <section> <cmdname>

Under Sun Solaris you would enter it like this

$ man -s <section> <cmdname>

where you replace <section> by the section number and <cmdname> by the command name.

Table 1. The standard man page sections

Section #Topic
1Commands available to users
2Unix and C system calls
3C library routines for C programs
4Special file names
5File formats and conventions for files used by Unix
7Word processing packages
8System administration commands and procedures

4.3. Pagers

Many times, the output of commands won't fit on your screen because it has more lines than your screen has. This is when pagers come into play. They can be used to “scroll” thru the output of commands.

The two most commonly used pagers are more and less. More is somewhat archaic, since you can scroll down only. Less is more user friendly. Both programs scroll down an entire screen when you press the SPACE key, and one line at a time when you press the RETURN key. They quit on the q key. When using less you can use your cursor keys to move up and down.

Both commands can be used to pipe output into them, as Example 15, “The usage of more or less” shows.

Example 15. The usage of more or less

$ ps ax | less
    0  ??  DLs    0:00.05 [swapper]
    1  ??  SLs    0:00.02 /sbin/init --
    2  ??  DL     0:01.50 [g_event]
    3  ??  DL     0:01.02 [g_up]
    4  ??  DL     0:00.95 [g_down]
    5  ??  DL     0:00.00 [xpt_thrd]
    6  ??  DL     0:00.00 [thread taskq]
    7  ??  DL     0:00.00 [kqueue taskq]
    8  ??  DL     0:00.00 [acpi_task_0]

For an explanation of the ps command, see Section 6, “Processes”. The important thing to grasp here, is that less takes the output from the ps command, and displays only a screen full of text and let you scroll thru the remaining text (more could have been used as well).