Commands, redirection, and the command line

When you login, you are presented with a command prompt generated by the command interpreter where you can interactively enter commands.

The general structure of a command is :

command [-options] [arguments] [< file_in] [file_out]

command - The command must be an internal shell command or a external program that the command interpreter can locate and that has been marked as executable. If the command is external to command interpreter, it must be found in one of the directories stored in the PATH variable or its location path must be stated as part of the command.

When a command is run without additional path information, the command interpreter first checks to see if the command is an internal function of itself. If it is not an internal command, the interpreter searches for the command in each directory listed in the shell's PATH variable. To see the current path settings, at the prompt, enter :

echo $PATH

The output will look something like :

/home/berezin/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games

This lists 6 different directories to look in for the command. The colon separate each directory path and name.

The search starts in the 1st directory listed and continues until either the command (and executed) is found or all directories listed are searched. It the search fail, the shell generates the appropriate error. You may add additional paths to the PATH variable if you wish. The following command resets the PATH variable to its original contents and appends a user's bin directory to the path list. This is the command for the bash shell.

PATH=$PATH:$HOME/bin If this is placed in the command shell's configuration file, it will be set each time the user logs in.

It is important to note that the 1st and only the 1st occurrence of the targeted command, will be run. If you wanted to explicitly run a command found in your home directory or from a directory other than the initially identified location, specify it before the command. If you suspect that more than one version of the command exists, use where and which to determine what is available in the PATH (where) and which will be run by default (which).

whereis test
which test

Caveat: if the command targeted is an internal shell routine, such as test or echo, whereis will not list it and which may not correctly report the correct default.

To run a program not in the path or a version other than the 1st one encountered in the PATH variable, specify the path as part of the command.

For example, a program called lister exists in the current directory, use :

./lister


Options and arguments.

The option requests a change in behavior from the default for a command. And arguments generally provide data or the location of needed data to the command. Although, in some cases, the options also take arguments.

Most commands have been designed with optional behavior but it is not a requirement. Traditionally, options are specified after the command but before arguments.

Currently, there are three styles for specifying options, not all styles may be recognized and some options may only be accessible if you use a specific style.

Standard Unix : The standard Unix style consists of a hyphen and a single character representing the option choice. If multiple options are specified, they may be listed independently or clustered together. However, certain options may be mutually exclusive, others may be order sensitive or require their own arguments. Always read the man page. The following examples invoke ps in a named user (-u) and generates a full listing (-f).

ps -f -u $USER

ps -fu $USER

ps -u $USER -f

All of these work the same. However :

ps -uf $USER

will fail because the $USER argument must be paired with -u.

Berkeley : The Berkeley option style uses just the character without the hyphen. For example, the Berkeley style's approximate equivalent to "ps -eF" is "ps aux". Fewer commands support the Berkeley style, but some do

GNU : The third option style is the GNU style. The GNU style uses a double hyphen and a word specifying the option. Some optional behavior may only be available through the GNU style options. The following uses the standard option to list every process and the GNU --forest option to organize the output a tree structure indicating the association between processes.

ps -e --forest | less

A command may support both standard and Berkeley style options, but you will probably not be able to mix them.

There is one additional option that many commands now support. It is the plain double hyphen "--". On occasion, you may need to pass an argument to a command that starts with a hyphen and not have it interpreted as an option. For instance, you have accidentally created the file -l in a directory. If you issue the command rm -l, you will get an invalid option error. the target is a file, specify its path. By prefixing the path, the hyphen is no longer the 1st character :

rm ./-l

Alternatively, use the double hyphen :

rm -- -l

Use the double hyphen after all desired options have been specified. This is the preceding command with the confirmation option invoked.

rm -i -- -l


Along with options, many commands take arguments. An option requests a change in the behavior of a command. An argument provides data to the command. In some cases, the argument is the data required by the command. For example, the echo command :

echo "Hello, world"

Often the argument[s] specify the source of any input required by the command and/or the destination of any output generated by the command. In these situations, you may use absolute or relative paths if the target source or destination is not the current working directory.

If either of these two pieces of information are not provided, many commands will assume input is read from standard input (stdin), the terminal keyboard, and output is to be sent to standard output (stdout), the terminal window.

You can see an example of this with the sort command.

(Run sort at the prompt with no options or arguments. Type in several words, pressing [enter] between words. When you have 4 or 5 words listed, press [ctrl]d to signal end of file or input. The words will list a second time on the screen in sorted order.)

sort
one
two
three
four
[ctrl]d

Because no options or arguments were provided to indicate either an input source or an output destination, sort uses the keyboard, standard input, and the screen, standard output.

Additionally, most commands are capable of sending error messages by a separate channel to standard error (stderr), also the terminal window.

Unix provides a mechanism called redirection that allows you to "redirect" standard input from a file rather than the keyboard and standard output and standard errors to files rather than to the terminal window. Redirection is useful when the input data for a command is already available in a file or you want to capture and preserve the output of a command.

Lectures