previous contents up next

Unix for Advanced Users

3. The Shell

3.3.1 Popular shells

3.3.1.1. Aliasing
The Bourne style syntax for aliases is

alias name="command"

The C-shell syntax for aliases is

alias name "command"

The quotation marks can be omitted, but are needed for aliases that contain spaces. For instance, you could write

alias l=ls,

but not

alias l=ls -al.

The latter would not work. To obtain the right effect, you would need to type

alias l="ls -al"

or

alias l "ls -al"

in the C-shell.

An alias can also contain environment variables that are expanded each time it is executed. To obtain this effect, you must enclose the target command (the right side of the name=command statement) in single quotation marks, to keep the command-line interpreter from expanding the variable before the alias is made. Hence alias g='echo $VAR' creates a shortcut command g that prints out the current value of VAR, but alias g="echo $VAR" creates a command that simply echos the value VAR had when the alias was made, regardless of whether you change VAR.

3.3.1.2. Job control
jobs is the command to list the jobs running as background processes of your current shell. It shows a job number in brackets, along with some information about the state of a given job (Running, Stopped, or Terminated). To see the process ID number of the jobs you type jobs -l

Shell commands that refer to jobs always use a percentage sign (%) before a job number in arguments. The most common example is fg, which brings a job into the foreground: its syntax is fg %job_number. Likewise, the command to take a stopped job and start it running again in the background, bg, is run as bg %job_number. kill can be used with a single job argument, as in kill %1 or with several, as in kill %1 %3 %5 %13.

3.3.1.3. I/O redirection
To redirect input to a command from a file, Bourne shells use the less-than symbol (<) and the greater-than sign (>) to redirect input from a command to a file. The latter is a more common case: for instance, you could egrep 'SECURITY|WARNING' /var/log/messages > bad_stuff to save the most serious messages from your syslog to a file. If you later wanted to append contents to that file without destroying the old contents, you could use the >> operator, e.g. egrep 'EMERGENCY|CATASTROPHIC' /var/log/messages >> bad_stuff. The idiom > filename_of_choice starts a new file with zero length, like touch, but also can destroy an existing file. You will often see it in scripts, but use it with caution on the command line.

While the above tokens are used to redirect standard input only, preceding either with a 2 will redirect standard error. find / -name '*important*' 2> /dev/null, for instance, will look for files you can reach on the system whose names match a pattern, while sending error messages about directories you are not permitted to read to oblivion. Likewise, you can redirect both standard error and standard input to a single location using the token 2>&1: startx 2>&1 debug-info, for instance, will start the X server under Linux, sending both streams to a file for later inspection. 2> and 1> can be used for the same command, as in my_daemon > daemon_output_log 2> daemon_error_log &.

Redirecting a command's input is less common, but useful for some commands. You can mail bob@sun.com < my_document to send a pre-composed message in a file called my_document.

3.3.1.4. Filename completion
In bash, you can type in a partial filename and hit the Tab key to complete it. The completion stops, sending a beep to the terminal, once an ambiguity is encountered. For instance, if you have a file called main.c and a directory called mail, hitting Tab after the partial command more m will cause the shell to fill the phrase in to more mai, then beep and show you the choices main.c and mail. You can then type in an n and hit Tab again to complete unambiguously the filename main.c. Alternately, you could type in l, which the shell would complete to mail, giving you a complete listing of that directory if you hit Tab a second time.

bash will also complete the names of executable commands for the first word you type at the command line, so that l followed by Tab prints a complete listing of commands in your path that begin with l. To keep from flooding your screen with command names, bash has a configurable maximum number of commands to show you which default to 100. If the number of available completions exceeds that number, it will prompt you asking whether it should really print them all.

sh and ksh do not perform filename completion.

3.3.1.5. History
ksh and bash have a built-in command called history that takes no arguments and prints out the last several commands you typed during the current shell session. The specific number of commands shown is configurable, like most shell attributes.

In addition, bash allows you to execute commands in your history either by number or by beginning characters, by preceding either value with an exclamation point (!). Hence !42 executes the forty-second command in your history; !vi will execute your last vi command, effectively reopening the last file you edited. Another bash feature lets you use the up and down arrow keys to scroll backwards and forwards in history, reviewing old commands and, if you choose, editing them and running them anew. You can even do Emacs-style isearches backwards and forwards, using Ctrl-r and Ctrl-s key sequences.

The more minimalistic ksh, like sh, lacks these features.

previous contents up next