This chapter functions as a reference source for C shell and Bourne, Korn, or POSIX shell features. Unlike other chapters of this guide that present conceptual or tutorial information, or both, the purpose of this chapter is to provide very brief reference information about each shell.
To get the most out this chapter, you already should be familiar with the introductory shell overview information in Chapter 7.
After completing this chapter, you should be able to:
Understand the main differences between operating system shells
Understand specific features of each operating system shell
Understand the specifics of local login scripts for each shell
8.1 Comparison of C, Bourne, Korn, and POSIX Shell Features
Table 8-1 compares selected features of the C shell and the Bourne, Korn, and POSIX shells.
Table 8-1: C, Bourne, Korn, and POSIX Shell Features
Feature | Description | C | Bourne | Korn or POSIX |
Shell programming | A programming language that includes features such as loops, condition statements, and variables. | Yes | Yes | Yes |
Signal trapping | Mechanisms for trapping interruptions and other signals sent by the operating system. | Yes | Yes | Yes |
Restricted shells | A security feature that provides a controlled shell environment with limited features. | No | Yes | No |
Command aliases | A feature that lets you abbreviate long command lines or to rename commands. | Yes | No | Yes |
Command history | A feature that stores commands and lets you edit and reuse them. | Yes | No | Yes |
File name completion | A feature that lets you enter a portion of a file name and the system automatically completes it or suggests a list of possible choices. | Yes | No | Yes |
Command line editing | A feature that lets you edit a current or previously entered command line. | Yes | No | Yes |
Array | The ability to group data and call it by a name. | Yes | No | Yes |
Integer arithmetic | The ability to perform arithmetic functions within the shell. | Yes | No | Yes |
Job control | Facilities for monitoring and accessing background processes. | Yes | No | Yes |
For detailed information on shell features, see the
appropriate shell reference pages
sh
(1b),
sh
(1p),
csh
(1), or
ksh
(1).
This section describes the following C shell features:
Sample
.cshrc
and
.login
scripts
Metacharacters
Command history and aliases
Built-in variables and commands
8.2.1 Sample .cshrc and .login Scripts
The
.cshrc
login script sets up your C shell environment
by defining variables and operating parameters for the local shell process.
The
.login
script defines variables and operating parameters
that you want executed at the beginning of your session, and that you want
to be valid for all shell processes during the current login session.
When you log in, the operating system executes the
.cshrc
file in your home directory first, and the
.login
file
second.
The
.login
script is executed only when you log
in.
However, the
.cshrc
file is executed each time you
create a subshell.
In
the following
.cshrc
script, shell variables, command aliases,
and command history variables are set.
Table 8-2
explains each
part of the script.
# Set shell variables set noclobber set ignoreeof set notify # Set command aliases alias h 'history \!* | more' alias l 'ls -l' alias c clear # Set history variables set history=40 set savehist=40 # Set prompt set prompt = "\! % "
Table 8-2: Example .cshrc Script
Command | Description |
Shell Variables | |
set noclobber |
Stops files from being overwritten.
If set,
places restrictions on output redirection
>
to ensure that
files are not accidentally destroyed, and that
>>
redirections
refer to existing files. |
set ignoreeof |
Specifies that you cannot use Ctrl/D
to end your login session.
Instead, you must use either the
exit
or the
logout
commands. |
set notify |
Informs you when background processes have completed. |
Command Aliases | |
alias h 'history \!* | more' |
Defines the contents of the command history
buffer through the
more
command.
The
\!*
string specifies that all the history buffer should be piped. |
alias l 'ls -l' |
Defines a short name,
l ,
for the
ls -l
command that lists directory files
in the long format. |
alias c clear |
Defines a short name,
c ,
for the
clear
command that clears your screen. |
History Variables | |
history=40 |
Instructs the shell to store the last 40 commands in the history buffer. |
savehist=40 |
Instructs the shell to store the last 40 commands and use them as the starting history for the next login session. |
Prompt Variable | |
set prompt = "\! % " |
Changes your prompt so that it tells you the command number of the current command. |
In the following
.login
script, the permissions for file creation are set, the
PATH
environment variable is set, and the editor and printer
are specified.
Table 8-3
explains each part of the script.
# Set file creation permissions umask 027 # Set environment variables set path=/usr/bin:/usr/local/bin: set cdpath=.:..:$HOME setenv EDITOR emacs setenv MAILHOST boston setenv PRINTER sales
Table 8-3: Example .login Script
Command | Description |
File Permissions | |
umask 027 |
Specifies the permissions to be subtracted
from the default permissions set by the creating program for all new files
created.
The
umask
value is subtracted from 777 (for executable
programs) or from 666.
For an executable program, a
umask
value of 027 results in all permissions for the owner, read and execute permissions
for members of the same group, and no permissions for all others. |
Environment Variables | |
set path \ /usr/bin:/usr/local/bin: |
Specifies the search path.
In this case,
/usr/bin
is searched first, and
/usr/local/bin
is searched second. |
set cdpath=.:..:$HOME |
The
cdpath
variable
sets the search path for the
cd
command.
This variable
assignment specifies that the
cd
command should search
for the named directory in the current directory (.) first, in the parent
directory (..) second, and the home directory ($HOME)
third. |
setenv EDITOR emacs |
Specifies the
emacs
editor
as the default editor when running a program that lets you edit a file.
For
example, various mail programs let you use an editor to compose and edit messages. |
setenv MAILHOST boston |
Specifies
boston
as your
mail handling system. |
setenv PRINTER sales |
Specifies the printer
sales
as your default printer. |
Table 8-4 describes C shell metacharacters (characters that have special meaning to the shell). The meaning of these metacharacters are grouped by interpretation when they appear in a shell script, in a Filename specification, when used to quote other characters, in an Input/Output specification, or when used to indicate variable substitution.
Table 8-4: C Shell Metacharacters
Metacharacter | Description |
Syntactic | |
; | Separates commands that should be executed sequentially. |
| | Separates commands that are part of a pipeline. |
&& | Runs the next command if the current command succeeds. |
| | | Runs the next command if the current command fails. |
( ) | Groups commands to run as a separate process in a subshell. |
& | Runs commands in the background. |
File Name | |
/ | Separates the parts of a file's pathname. |
? | Matches any single character except a leading dot (.). |
* | Matches any sequence of characters except a leading dot (.). |
[ ] | Matches any of the enclosed characters. |
~ | Specifies a home directory when used at the beginning of pathnames. |
Quotation | |
'...' | Specifies that any of the enclosed characters should be interpreted literally; that is, without their special meaning to the shell. |
"..." | Provides a special form of quoting. Specifies that the $ (dollar sign), ` (grave accent), and \ (backslash) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes are useful in making variable assignments. |
Input/Output | |
< | Redirects input. |
> | Redirects output to a specified file. |
<< | Redirects input and specifies that the shell should read input up to a specified line. |
>> | Redirects output and specifies that the shell should add output to the end of a file. |
>& | Redirects both diagnostic and standard output and appends them to a file. |
>>& | Redirects both diagnostic and standard output to the end of an existing file. |
>! | Redirects ouput and specifies that if the noclobber variable is set (prevents overwriting of files); it should be ignored so that the file can be overwritten. |
Substitution | |
$ | Specifies variable substitution. |
! | Specifies history substitution. |
: | Precedes substitution modifiers. |
^ | Used in special kinds of history substitution. |
` | Specifies command substitution. |
The command history buffer stores the commands you enter and lets you display them at any time. As a result, you can select a previous command, or parts of previous commands, and then reexecute them. This feature may save you time because it lets you reuse long commands instead of reentering them.
You may want to enter the following three commands in your
.cshrc
file:
set history=n
Creates a history buffer that stores the command lines you enter. The n entry specifies the number of command lines you want to store in the history buffer.
set savehist=n
Saves the command lines you entered during the current login session and makes them available for the next login session. The n entry specifies the number of command lines you want to store in the history buffer when you log out.
set prompt=[\!] %
Causes your C shell prompt to display the number of each command line.
To see the contents of the history buffer, use the
history
command.
The displayed output will be similar to the following
(your output will vary):
[18] %
history
3 set history=15
4 pwd
5 cd /usr/sales
6 ls -l
7 cp report report5
8 mv /usr/accounts/new .
9 cd /usr/accounts/new
10 mkdir june
11 cd june
12 mv /usr/accounts/new/june .
13 ls -l
14 cd /usr/sales/Q1
15 vi earnings
16 cd /usr/chang
17 vi status
18 history
[19] %
To reexecute any command in the command history buffer, use the commands listed in Table 8-5. Each command starts with an exclamation point (!), which tells the C shell that you are using commands in the history buffer.
Table 8-5: Reexecuting History Buffer Commands
Command | Description |
!! |
Reexecutes the previous command. |
!n |
Reexecutes the command specified by
n.
For example, using the history buffer shown in the previous
display,
!5
reexecutes the
cd /usr/sales
command. |
!-n |
Reexecutes a previous command relative to
the current command.
For example, using the history buffer shown in the previous
display,
!-2
invokes command number
17 ,
vi status . |
!string |
Reexecutes the most recent command that has
first characters matching those specified by
string.
For example, using the history buffer shown in the previous display,
!cp
invokes command number
7 ,
cp report
report5 . |
!?string |
Reexecutes the most recent command line that
has any characters matching those specified by
string.
For example, using the history buffer shown in the previous display,
!?Q1
invokes command number
14 ,
cd /usr/sales/Q1 . |
The command history buffer also lets you reuse previous command arguments
as well as modify previous command lines.
For information on these features,
see the
csh
(1)
reference page.
The C shell lets you enter a portion of a file name or pathname at the shell prompt, and the shell automatically will match and complete the name. This feature saves you time when you are trying to display long, unique file names.
For example, assume that you have the file
meetings_sales_status
in your current directory.
To display a long listing of the file,
enter the following command:
%
ls -l meetings[Escape]
The system displays the following on the same command line:
%
ls -l meetings_sales_status
You can now execute the command by pressing Return.
For more detailed information on file name completion, see the
csh
(1)
reference page.
8.2.5 Aliases
The command aliases feature lets you abbreviate long command lines or
rename commands.
You do this by creating
aliases
for long
command lines that you frequently use.
For example, assume that you often need to move to the directory
/usr/chang/reports/status
.
You can create an alias
status
, which will move you to that directory whenever you enter it on
the command line.
In addition, aliases let you make up more descriptive names for commands.
For example, you could define an alias named
rename
for
the
mv
command.
To create aliases, use the
alias
command.
The format
of the
alias
command is:
alias
aliasname command
The
aliasname
entry specifies the
name you want to use.
The
command
entry specifies
either the original command or a series of commands.
If the
command
has more than one part (has spaces), enclose the whole
expression in single quotes (' '
).
For example, to create the alias
status
that moves
you to the directory
/usr/chang/reports/status
, enter the
following command:
%
alias status 'cd /usr/chang/reports/status'
The usual
way to define aliases is to make them a permanent part of your environment
by including them in your
.cshrc
file.
As a result, you
can use the aliases whenever you log in or start a new shell.
See
Section 8.2.1
for an example.
To display all alias definitions, enter the following command:
%
alias
To display the definition of a particular alias, enter the following command:
%
alias aliasname
The aliasname entry specifies the particular alias for which you are requesting a definition.
To remove an alias for the current login session, use the
unalias
command.
The general format of the
unalias
command is the following:
unalias
aliasname
The aliasname entry specifies the alias you want to remove.
To remove an alias for the current and all future login sessions, do the following:
Enter the following command:
%
unalias aliasname
The aliasname entry specifies the alias you want to remove.
Edit the
.cshrc
file and remove the alias
definition.
Then, save the file.
Enter the following command to reexecute the
.cshrc
file:
%
source .cshrc
For complete information on using aliases with the C shell, see the
csh
(1)
reference page.
8.2.6 Built-In Variables
The C shell provides variables that can be assigned values. These variables can be very useful for storing values that can be used later in commands. In addition, you can affect directly shell behavior by setting those variables to which the shell itself refers.
Table 8-6
describes selected C shell built-in variables
that are of the most interest to general users.
For a complete list of C shell
built-in variables, see the
csh
(1)
reference page.
Table 8-6: C Shell Built-In Variables
Variable | Description |
|
Contains a value or values that can be used by the shell or shell scripts. |
|
Contains the pathname to your current
directory.
The value of this variable changes every time you use the
|
|
Contains the pathname of your home
directory.
The default value for this variable is specified in the
|
|
Specifies whether Ctrl/D can
be used to log out from the system.
If set, you must use either
|
|
Specifies alternative directories to
be searched by the system when locating subdirectories with the
|
|
Specifies whether a file can be overwritten.
If set, places restrictions on output redirection
|
|
Specifies whether you want to be notified
when a background process has completed.
If set, you are notified; if unset,
you are not notified.
This variable is usually set in the
|
|
Specifies the search path that the
shell uses to find commands.
This variable is usually set in the
|
|
Can be used to customize your C shell
prompt.
This variable is usually set in the
|
|
Specifies the shell to create when
a program creates a subshell.
This variable is usually set in the
|
status |
Specifies whether the most recently executed command completed without error (a value of zero is returned) or with an error (a nonzero value is returned). |
Table 8-7
describes selected C shell commands that
are of the most interest to general users.
For a complete list of C shell
built-in commands, or for more information on the commands listed here, see
the
csh
(1)
reference page.
Table 8-7: Built-In C Shell Commands
Command | Description |
|
Assigns and displays alias definitions. [Footnote 2] |
|
Puts a suspended process in the background. [Footnote 3] |
|
Writes arguments to the shell's standard output. |
|
Puts a currently running background process in the foreground. [Footnote 3] |
|
Displays the contents of the command history buffer. [Footnote 4] |
|
Displays the job number and the PID number of current background processes. [Footnote 3] |
|
Terminates the login session. |
|
Tells the shell to recompute the hash
table of command locations.
Use this command if you add a command to a directory
in the shell's search path and want the shell to be able to find it.
If you
do not use
|
|
Repeats a command a specified number of times. |
|
Assigns and displays shell variable values. [Footnote 5] |
|
Assigns environment variable values. [Footnote 5] |
|
Executes commands in a file. This can be used to update the current shell environment. [Footnote 6] |
|
Displays the execution time of a specified command. |
|
Removes alias definitions. [Footnote 2] |
|
Removes values that have been assigned to variables. [Footnote 5] |
|
Removes values that have been assigned to environment variables. [Footnote 5] |
This section describes the following Bourne shell features:
A sample
.profile
login script
Metacharacters
Built-in variables
Built-in commands
8.3.1 Sample .profile Login Script
If your login shell is the Bourne shell, the operating system executes
the
.profile
login script to set up your environment.
The
.profile
login script variables that are exported
are passed to any subshells and subprocesses that are created.
Variables that
are not exported are used only by the login shell.
In
the following
.profile
login script, shell variables
are set and exported, a trap is set for the logout script, and the system
is instructed to display information.
Table 8-8
explains each
part of the script.
# Set PATH PATH=/usr/bin:/usr/local/bin: # Export global variables export PATH # Set shell variables PS1='$LOGNAME $ ' CDPATH=.:..:$HOME # Set up for logout script trap "echo logout; $HOME/.logout" 0 # Display status information date echo "Currently logged in users:" ; users
Table 8-8: Example Bourne Shell .profile Script
Command | Description |
Set Search Path | |
PATH=/usr/bin:/usr/local/bin: |
Specifies the search path.
In this case,
/usr/bin
is searched first and
/usr/local/bin
searched second. |
Export Search Path | |
export PATH |
Specifies that the search path is to be passed to all commands that you execute. |
Set Shell Variables | |
PS1='$LOGNAME $ ' |
The
PS1
variable
specifies the Bourne shell prompt, and its default value is
$ .
However, this variable assignment specifies that your prompt should be changed
to the following:
username
$ .
For example, if your user name were
amy , your prompt would
be the following:
amy $ . |
CDPATH=.:..:$HOME
|
The
CDPATH
variable
sets the search path for the
cd
command.
This variable
assignment specifies that the
cd
command should search
for the named directory in the current directory (.) first, in the parent
directory (..) second, and the home directory ($HOME)
third. |
Set Up Logout Script | |
trap "echo logout;
$HOME/.logout" 0
|
Specifies that your shell should display
logout
and execute your
.logout
script when the
trap
command captures the exit signal (0).
[Footnote 7]
|
Display Status Information | |
date
|
Displays the date and time. |
Table 8-9 describes Bourne shell metacharacters (characters that have special meaning to the shell). The meaning of these meta characters are grouped by interpretation when they appear in a shell script, in a Filename specification, when used to quote other characters, in an Input/Output specification, or when used to indicate variable substitution.
Table 8-9: Bourne Shell Metacharacters
Metacharacter | Description |
Syntactic |
|
| |
Separates commands that are part of a pipeline. |
&& |
Runs the next command if current command succeeds. |
| | |
Runs the next command if the current command fails. |
; |
Separates commands that should be executed sequentially. |
;; |
Separates elements of a case construct. |
& |
Runs commands in the background. |
( ) |
Groups commands to run as a separate process in a subshell. |
File Name |
|
/ |
Separates the parts of a file's pathname. |
? |
Matches any single character except a leading dot (.). |
* |
Matches any sequence of characters except a leading dot (.). |
[ ] |
Matches any of the enclosed characters. |
Quotation |
|
\ |
Specifies that the following character should be interpreted literally; that is, without its special meaning to the shell. |
'...' |
Specifies that any of the enclosed
characters (except for the
|
"..." |
Provides a special form of quoting. Specifies that the $ (dollar sign), ` (grave accent), and \(backslash) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes are useful in making variable assignments. |
Input/Output |
|
< |
Redirects input. |
> |
Redirects output to a specified file. |
<< |
Redirects input and specifies that the shell should read input up to a specified line. |
>> |
Redirects output and specifies that the shell should add output to the end of a file. |
2> |
Redirects diagnostic output to a specified file. |
Substitution |
|
${...} |
Specifies variable substitution. |
`...` |
Specifies command output substitution. |
The Bourne shell provides variables that can be assigned values. The shell sets some of these variables, and you can set or reset all of them.
Table 8-10
describes selected Bourne shell built-in variables
that are of most interest to general users.
For complete information on all Bourne shell
built-in variables, see the
sh
(1b)
reference page.
Table 8-10: Bourne Shell Built-In Variables
Variable | Description |
Specifies the name of your login directory,
the directory that becomes the current directory upon completion of a login.
The
|
|
Specifies the directories through which
your system should search to find and execute commands.
The shell searches
these directories in the order specified here.
Usually, The
|
|
Specifies the directories that the
|
|
The pathname of the file where your
mail is deposited.
You must set
|
|
Specifies in seconds how often the
shell checks for mail (600 seconds is the default).
If the value of this variable
is set to 0, the shell checks for mail before displaying each prompt.
Usually
|
|
Specifies your default shell.
This
variable should be set and exported by your
|
|
Specifies the default Bourne shell prompt.
Its default value is
|
|
Specifies the secondary prompt string --
the string that the shell displays when it requires more input after you enter
a command line.
The standard secondary prompt string is a
|
Table 8-11
describes selected Bourne shell commands that
are of the most interest to general users.
For a complete list of Bourne shell
built-in commands, see the
sh
(1b)
reference page.
Table 8-11: Bourne Shell Built-In Commands
Command | Description |
Lets you change directories.
If no
directory is specified, the value of the
|
|
Writes arguments to the standard output. [Footnote 9] |
|
Marks the specified variable for automatic export to the environments of subsequently executed commands. |
|
Displays the current directory. [Footnote 10] |
|
Assigns and displays variable values. [Footnote 11] |
|
Displays the accumulated user and system times for processes run from the shell. |
|
Runs a specified command when the shell receives a specified signal. [Footnote 11] |
|
Specifies the permissions to be subtracted for all new files created. [Footnote 12] |
|
Removes values that have been assigned to variables. [Footnote 11] |
8.4 Korn or POSIX Shell Features
The POSIX shell is another designator for the Korn shell that signifies compliance with the IEEE POSIX.2 standard. This section describes the following Korn or POSIX shell features:
Sample
.profile
and
.kshrc
login scripts
Metacharacters
Command history
Editing command lines
File name completion
Aliases
Built-in variables and commands
8.4.1 Sample .profile and .kshrc Login Scripts
If your login shell is the Korn or POSIX shell, the operating system processes
the
.profile
login script in your home directory.
The
.profile
login script defines environment variables.
These variables
are used by your login shell as well as any subshells and subprocess that
are created.
The
.profile
login script is executed only
when you log in.
The
.kshrc
login script sets up your Korn or POSIX shell
environment by defining variables and operating parameters for the local shell
process.
It is executed each time you create a subshell.
Note
Before creating a
.kshrc
file in your home directory, make sure that theENV=$HOME/.kshrc
environment variable is set and exported in your.profile
. Once this is done, the.kshrc
login script will execute each time you log in and each time you create a subshell.
In
the following
.profile
login script, global environment
variables are set and exported, and shell variables are set.
Table 8-12
explains each part of the script.
# Set environment variables PATH=/usr/bin:/usr/local/bin: ENV=$HOME/.kshrc EDITOR=vi FCEDIT=vi PS1="'hostname' [!] $ " # Export global variables export PATH ENV EDITOR FCEDIT PS1 # Set mail variables MAIL=/usr/spool/mail/$LOGNAME MAILCHECK=300
Table 8-12: Example Korn or POSIX Shell .profile Script
Command | Description |
Set Environment Variables | |
PATH=/usr/bin:/usr/local/bin |
Specifies the search path.
In this case,
/usr/bin
is searched first and
/usr/local/bin
is searched second. |
ENV=$HOME/.kshrc |
Specifies
$HOME/.kshrc
as the login script. |
EDITOR=vi |
Specifies
vi
as the default
editor for command line editing at the shell prompt and for file name completion. |
FCEDIT=vi |
Specifies
vi
as the default
editor for the
fc
command.
[Footnote 13]
|
PS1="`hostname` [!] $ " |
The
PS1
variable
specifies the Korn or POSIX shell prompt,.
Its default value is
$ .
This variable assignment specifies that your prompt should be changed to the
following: the output of the
hostname
command, followed
by the command number of the current command, followed by the dollar sign
($).
For example, if the name of your system is
boston ,
and the current command is numbered
30 , your prompt would
be the following:
boston[30] $ . |
Export Global Variables | |
export PATH ENV EDITOR FCEDIT
PS1 |
Specifies that the values of the PATH, ENV, EDITOR, FCEDIT, and PS1 variables should be exported to all subshells. |
Set Mail Variables | |
MAIL=/usr/spool/mail/$LOGNAME |
Specifies the pathname of the file used by
the mail system to detect the arrival of new mail.
In this case, the mail
system would look in your user name subdirectory under the
/usr/spool/mail
directory. |
MAILCHECK=300 |
Specifies that the shell should check for mail every 300 seconds (5 minutes). |
In
the following
.kshrc
login script, shell variables, command
aliases, and command history variables are set, as well as the permissions
for file creation.
Table 8-13
explains each part of the script.
# Set shell variables set -o monitor set -o trackall # Set command aliases alias rm='rm -i ' alias rename='mv ' alias l 'ls -l' alias c clear # Set history variables HISTSIZE=40 # Set file creation permissions umask 027
Table 8-13: Example .kshrc Script
Command | Description |
Shell Variables | |
set -o monitor |
Specifies that the shell should monitor all background processes and display a completion message when the process finishes. |
set -o trackall |
Specifies that the shell should track all commands that you execute. Once a command is tracked, the shell stores the location of the command and finds the command more quickly the next time you enter it. |
Command Aliases | |
alias rm='rm -i' |
Specifies the use of the
-i
option (which prompts you for file deletion) with the
rm
command. |
alias rename='mv' |
Specifies
rename
as a
new name for the
mv
command. |
alias l='ls -l' |
Defines a short name for the
ls -l
command that lists directory files in the long format. |
alias c='clear' |
Defines a short name for the clear command that clears your screen. |
History Variables | |
HISTSIZE=40 |
Instructs the shell to store the last 40 commands in the history buffer. |
Set File Creation Permissions | |
umask 027 |
Specifies the maximum permissions for all new files created. This command provides all permissions for the owner, read, and execute permissions for members of the same group, and no permissions for all others. The umask is not inherited by subshells. |
Table 8-14 describes Korn or POSIX shell metacharacters (characters that have special meaning to the shell). The meaning of these meta characters are grouped by interpretation when they appear in a shell script, in a Filename specification, when used to quote other characters, in an Input/Output specification, or when used to indicate variable substitution.
Table 8-14: Korn or POSIX Shell Metacharacters
Metacharacter | Description |
Syntactic |
|
| |
Separates commands that are part of a pipeline. |
&& |
Runs the next command if the current command succeeds. |
| | |
Runs the next command if the current command fails. |
; |
Separates commands that should be executed sequentially. |
;; |
Separates elements of a case construct. |
& |
Runs commands in the background. |
( ) |
Groups commands in a subshell as a separate process. |
{ } |
Groups commands without creating a subshell. |
File Name |
|
/ |
Separates the parts of a file's pathname. |
? |
Matches any single character except a leading dot (.). |
* |
Matches any character sequence except a leading dot (.). |
[ ] |
Matches any of the enclosed characters. |
~ |
Specifies a home directory when it begins a file name. |
Quotation |
|
\ |
Specifies that the following character should be interpreted literally; that is, without its special meaning to the shell. |
'...' |
Specifies that any of the enclosed
characters (except for the
|
"..." |
Provides a special form of quoting. Specifies that the dollar sign ($), ` (grave accent), \ (backslash), and ) (close parenthesis) characters keep their special meaning, while all other enclosed characters are interpreted literally; that is, without their special meaning to the shell. Double quotes (" ") are useful in making variable assignments. |
Input/Output |
|
< |
Redirects input. |
> |
Redirects output to a specified file. |
<< |
Redirects input and specifies that the shell should read input up to a specified line. |
>> |
Redirects output and specifies that the shell should add output to the end of a file. |
>& |
Redirects both diagnostic and standard output and appends them to a file. |
Substitution |
|
${...} |
Specifies variable substitution. |
% |
Specifies job number substitution. |
`...` |
Specifies command output substitution. |
The command history buffer stores the commands you enter and lets you display them at any time. As a result, you can select a previous command, or parts of previous commands, and then reexecute them. This feature may save you time because it lets you reuse long commands instead of reentering them.
To see the contents of the history buffer, use the
history
command.
The displayed output will be similar to
Example 8-1
(your output will vary).
Example 8-1: Sample ksh history Output
[18] $
history
3 ls -l
4 pwd
5 cd /usr/sales
6 ls -l
7 cp report report5
8 mv /usr/accounts/new .
9 cd /usr/accounts/new
10 mkdir june
11 cd june
12 mv /usr/accounts/new/june .
13 ls -l
14 cd /usr/sales/Q1
15 vi earnings
16 cd /usr/chang
17 vi status
[19] $
To reexecute any
command in the command history buffer, use the commands listed in
Table 8-15.
Each command starts with the letter
r
.
Table 8-15: Reexecuting History Buffer Commands
Command | Description |
r |
Reexecutes the previous command. |
r
n |
Reexecutes the command specified by
n.
For example, using the history buffer shown in the previous
display,
r 5
reexecutes the
cd /usr/sales
command. |
r -n |
Reexecutes a previous command relative to
the current command.
For example, using the history buffer shown in the previous
display,
r-2
invokes command number
16 ,
cd /usr/chang . |
r
string |
Reexecutes the most recent command that has
first characters matching those specified by
string.
For example, using the history buffer shown in the previous display,
r cp
invokes command number
7 ,
cp report
report5 . |
For more information on reexecuting history buffer commands, see the
ksh
(1)
reference page.
If you want to increase or decrease the number of commands stored in
your history buffer, set the
HISTSIZE
variable in
your
.profile
file.
This variable has the following format:
HISTSIZE=
n
The n entry specifies the number of command lines you want to store in the history buffer.
For example, to store 15 commands in the history buffer, use the following command:
HISTSIZE=15
The Korn or POSIX shell also lets you edit current command lines as well as reuse
those already entered in the command history buffer.
To use this feature,
you must know how to use a text editor such as
vi
or
emacs
.
For information on these features, see the following section.
8.4.4 Command Line Editing Using the fc Command
The Korn or POSIX shell lets you list or edit or both the command lines in your command history buffer. As a result, you may modify any element of a previous command line and then reexecute the command line.
The command line editing functions for the Korn or POSIX shell are extensive.
This section covers only the most basic functions.
For more detailed information,
see the
ksh
(1)
or
sh
(1p)
reference pages.
To display the command history buffer or to edit its contents, or both,
use the built-in command
fc
(fix command).
The
fc
command has the following two formats:
fc
[ -e editor
]
[ -nlr
]
[ first
]
[ last
]
This command format lets you display and edit any number of command lines in your buffer.
The
-e
editor
entry specifies the editor (usually
vi
or
emacs
) you want to use in editing the command line.
If you do not specify
-e
, the
fc
command displays the lines,
but does not let you edit them.
The
-n
flag specifies that you want
to list the command lines in the buffer without numbers.
The
-l
flag specifies that you want to list the command lines in the buffer
with numbers.
If you do not specify a line number or a range of line numbers,
the last 16 lines you entered will be listed.
The
-r
flag specifies that you want
to list the command in the buffer in reverse order.
The first and last entries specify a range of command lines in the buffer. You may specify them either with numbers or with strings.
If you want to specify a default editor for the
-e
flag, define the
FCEDIT
variable in
your
.profile
script.
For example, if you want to make
emacs
your default editor, enter the following variable definition:
FCEDIT=emacs
fc
-e -
[old=new
]
[string
]
This command lets you immediately replace an old string with a new string within any previous command line.
The
-e -
entry specifies that
you want to make a replacement.
The old=new entry specifies that you want to replace the old string with the new string.
The string entry specifies that the Korn or POSIX shell should make the edit to the most recent command line in the buffer containing the string.
The following section contains some examples of
fc
use.
The Korn or POSIX shell also lets you edit individual command lines at the
shell prompt by using a command set similar to the
vi
or
the
emacs
editors.
For more information on this feature,
see the
ksh
(1)
or
sh
(1p)
reference pages.
8.4.4.1 Examples of Command Line Editing
To display command lines 15 to 18, enter the following command:
$
fc -l 15 18
15 ls -la
16 pwd
17 cd /u/ben/reports
18 more sales
$
You also may list the same command lines by specifying command strings instead of line numbers, as in the following example:
$
fc -l ls more
15 ls -la
16 pwd
17 cd /u/ben/reports
18 more sales
$
To display and edit command lines 15 to 18 with the
vi
editor, enter the following command:
$
fc -e vi 15 18
ls -la
pwd
cd /u/ben/reports
more sales
~
~
~
~
After making your edits, write
and exit the file with the
:wq!
command.
The command lines
in the file are then reexecuted.
Assume that you have just entered the
echo hello
command, and now want to replace
hello
with
goodbye
.
To do the replacement and reexecute the command line, enter the
following command:
$
echo hello
hello
$
fc -e - hello=goodbye echo
echo goodbye
goodbye
For more detailed information
on the
fc
command and command line editing, see the
ksh
(1)
reference page.
The Korn or POSIX shell lets you enter a portion of a file name or pathname at the shell prompt and the shell automatically will match and complete the name. If there is more than one file name or pathname that matches the criterion, the shell will provide you with a list of possible matches.
To activate the file name completion mechanism, define the
EDITOR
variable in your
.profile
file.
For
example, if you want to use the
vi
editor, enter the following
variable definition in your
.profile
file:
EDITOR=vi
To demonstrate how file name completion
works, assume that your editor is
vi
and that you have
the
salesreport1
,
salesreport2
, and
salesreport3
files in your current directory.
To display a long
listing and to activate file name completion, enter the following command:
$
ls -l salesreport [Escape] =
1) salesreportfeb
2) salesreportjan
3) salesreportmar
$ ls -l salesreport
The system
redisplays your command, and the cursor is now at the end of
salesreport
.
If you want to choose
salesreportjan
, type
a
(the
vi
append command) followed by
jan
, then press Return.
The listing for
salesreportjan
will be displayed.
For more detailed information on file name completion, see the
ksh
(1)
and
sh
(1p)
reference page.
8.4.6 Aliases
The command aliases feature lets you abbreviate command lines or rename
commands.
You do this by creating
aliases
for command lines
that you frequently use.
For example, assume that you often need to move to the directory
/usr/chang/reports/status
.
You can create an alias
status
, which will move you to that directory whenever you enter it on
the command line.
In addition, aliases let you make up more descriptive names for commands.
For example, you could define an alias named
rename
for
the
mv
command.
To create aliases, use the
alias
command.
The general
format of the
alias
command is the following:
alias
aliasname=command
The aliasname entry specifies the name you want to use. The command entry specifies either the original command or a series of commands. If the command has more than one part (has spaces), enclose the whole expression in single quotes.
For example, to create the alias
status
that moves
you to the directory
/usr/chang/reports/status
, enter the
following command:
alias status='cd /usr/chang/reports/status'
The usual way to define aliases is to place them in
your
.kshrc
file so that you can use them whenever you
log in or start a new shell.
See
Section 8.4.1
for an example.
To display all alias definitions, enter the following command:
$
alias
To display the definition of a particular alias, enter the following command:
$
alias aliasname
The aliasname entry specifies the particular alias for which you are requesting a definition.
The Korn or POSIX shell lets you export the aliases you create. Aliases that are exported are passed to any subshells that are created so that when you execute a shell procedure or new shell, the alias remains defined. (Aliases that are not exported are used only by the login shell.)
To export an alias, use the following form of the
alias
command:
alias -x
aliasname=command
The
-x
flag specifies that you want
to export the alias.
The
aliasname
entry specifies
the name you want to use.
The
command
entry specifies
either the original command or a series of commands.
If the
command
has more than one part, enclose the whole expression
in single quotes.
For example, to export an alias definition for the
rm
command, enter the following:
alias -x rm='rm -i '
You can enter the preceding command in one of two ways:
Edit the
.kshrc
or
.profile
file if you want an alias exported whenever you log in
Export an alias on the command line if you want the alias exported only for the current login session
To remove an alias for the current login session, use the
unalias
command.
The general format of the
unalias
command is the following:
unalias
aliasname
The aliasname entry specifies the alias you want to remove.
To remove an alias for the current and all future login sessions, do the following:
Enter the following command:
$
unalias aliasname
The aliasname entry specifies the alias you want to remove.
Edit the
.kshrc
file (or the file on your
system that contains alias definitions) and remove the alias definition.
Then,
save the file.
Enter the following command to reexecute the
.kshrc
file:
$
. ./.kshrc
The Korn or POSIX shell provides additional aliasing features.
For complete
information on using aliases with the Korn or POSIX shell, see the
ksh
(1)
or
sh
(1p)
reference pages.
8.4.7 Built-In Variables
The Korn and POSIX shells provide variables that can be assigned values. The shell sets some of these variables, and you can set or reset all of them.
Table 8-16
describes selected Korn or POSIX shell built-in
variables that are of the most interest to general users.
For complete information
on all Korn or POSIX shell built-in variables, see the
ksh
(1)
or
sh
(1p)
reference pages.
Table 8-16: Built-In Korn or POSIX Shell Variables
Variable | Description |
HOME
|
Specifies the name of your login directory.
The
cd
command uses the value of
HOME
as its default value.
In Korn or POSIX shell procedures, use
HOME
to avoid having to use full pathnames -- something that is especially
helpful if the pathname of your login directory changes.
The
HOME
variable is set by the
login
command. |
PATH
|
Specifies the directories through which your
system should search to find and execute commands.
The shell searches these
directories in the order specified here.
Usually,
PATH
is set in the
.profile
file. |
CDPATH
|
Specifies the directories that the
cd
command will search to find the specified argument to
cd .
If the
cd
argument is null, or if it begins
with a slash ( / ), dot (.), or dot dot (..), then
CDPATH
is ignored.
Usually,
CDPATH
is
set in your
.profile
file. |
MAIL
|
The pathname of the file where your mail
is deposited.
Usually,
MAIL
is set in your
.profile
file. |
MAILCHECK
|
Specifies in seconds how often the shell
checks for mail (600 seconds is the default).
If the value of this variable
is set to 0, the shell checks for mail before displaying each prompt.
Usually,
MAILCHECK
is set in your
.profile
file. |
SHELL
|
Specifies your default shell.
This variable
should be set and exported by your
.profile
file. |
PS1
|
Specifies the Korn or POSIX shell prompt.
Its
default value is the dollar sign ($).
The
PS1
variable
is usually set in your
.profile
file. |
PS2
|
Specifies the secondary prompt string -
the string that the shell displays when it requires more input after entering
a command line.
The standard secondary prompt string is a
>
symbol followed by a space.
The variable
PS2
is usually
set in your
.profile
file. |
HISTFILE
|
Specifies the pathname of the file that will
be used to store the command history.
This variable is usually set in your
.profile
file. |
EDITOR
|
Specifies the default editor for command
line editing at the shell prompt and for file name completion.
This variable
is usually set in your
.profile
file. |
FCEDIT
|
Specifies the default editor for the
fc
command.
This variable is usually set in your
.profile
file. |
HISTSIZE
|
Specifies the number of previously entered
commands that are accessible by this shell.
The default is 128.
This variable
is usually set in your
.kshrc
file. |
Table 8-17
describes selected Korn or POSIX shell commands
that are of the most interest to general users.
For a complete list of shell
built-in commands, or for more information on the commands listed, see the
ksh
(1)
or
sh
(1p)
reference pages.
Most of these commands also have a reference
page that you can access as described in
Section 1.6.1.
Table 8-17: Korn or POSIX Shell Built-In Commands
Command | Description |
|
Assigns and displays alias definitions.
For more information about
the
|
Lets you change directories.
If no
directory is specified, the value of the
|
|
Writes arguments to the standard output. |
|
Marks the specified variable for automatic
export to the environments of subsequently executed commands.
For more information
about the
|
|
Lets you display, edit, and reexecute
the contents of the command history buffer.
For more information about the
|
|
Displays the contents of the command
history buffer.
For more information about the
|
|
Displays the job number and the PID
number of current background processes.
For more information about the
|
|
Displays the current directory.
For
more information about the
|
|
Assigns and displays variable values.
For more information about the
|
|
Displays the accumulated user and system times for processes run from the shell. |
|
Runs a specified command when the shell
receives a specified signal.
For more information about the
|
|
Specifies the permissions to be subtracted
from the default permissions set by the creating program for all new files
created.
For more information about the
|
|
Removes alias definitions.
For more
information about the
|
|
Removes values that have been assigned
to variables.
For more information about the
|