1994-05-26 06:18:55 +00:00
|
|
|
.\" Copyright (c) 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to Berkeley by
|
|
|
|
.\" Kenneth Almquist.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the University of
|
|
|
|
.\" California, Berkeley and its contributors.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
1997-04-28 03:20:09 +00:00
|
|
|
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
|
1999-08-27 23:15:48 +00:00
|
|
|
.\" $FreeBSD$
|
1994-05-26 06:18:55 +00:00
|
|
|
.\"
|
1997-04-28 03:20:09 +00:00
|
|
|
.Dd May 5, 1995
|
|
|
|
.Dt SH 1
|
|
|
|
.Os BSD 4
|
|
|
|
.Sh NAME
|
|
|
|
.Nm sh
|
|
|
|
.Nd command interpreter (shell)
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Nm
|
1999-04-02 09:28:00 +00:00
|
|
|
.Op Fl /+abCEefIimnpsTuVvx
|
1997-04-28 03:20:09 +00:00
|
|
|
.Op Fl /+o Ar longname
|
1997-09-13 17:40:00 +00:00
|
|
|
.Op Fl c Ar string
|
2000-12-18 14:48:17 +00:00
|
|
|
.Op Ar arg ...\&
|
1997-04-28 03:20:09 +00:00
|
|
|
.Sh DESCRIPTION
|
1999-11-18 08:13:21 +00:00
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
utility is the standard command interpreter for the system.
|
1997-04-28 03:20:09 +00:00
|
|
|
The current version of
|
|
|
|
.Nm
|
|
|
|
is in the process of being changed to
|
|
|
|
conform with the
|
|
|
|
.St -p1003.2
|
|
|
|
specification for the shell. This version has many features which make
|
|
|
|
it appear
|
1994-05-26 06:18:55 +00:00
|
|
|
similar in some respects to the Korn shell, but it is not a Korn
|
1999-11-10 13:27:54 +00:00
|
|
|
shell clone like
|
|
|
|
.Xr pdksh 1 .
|
1999-10-07 13:56:43 +00:00
|
|
|
Only features
|
1994-05-26 06:18:55 +00:00
|
|
|
designated by POSIX, plus a few Berkeley extensions, are being
|
1997-04-28 03:20:09 +00:00
|
|
|
incorporated into this shell.
|
1999-10-07 13:56:43 +00:00
|
|
|
This man page is not intended to be a tutorial nor a complete
|
1994-05-26 06:18:55 +00:00
|
|
|
specification of the shell.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Overview
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell is a command that reads lines from
|
|
|
|
either a file or the terminal, interprets them, and
|
1999-09-14 11:46:04 +00:00
|
|
|
generally executes other commands.
|
1999-10-07 13:56:43 +00:00
|
|
|
It is the program that is started when a user logs into the system,
|
|
|
|
although a user can select a different shell with the
|
1999-09-08 15:40:46 +00:00
|
|
|
.Xr chsh 1
|
1999-10-07 13:56:43 +00:00
|
|
|
command.
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell
|
|
|
|
implements a language that has flow control constructs,
|
|
|
|
a macro facility that provides a variety of features in
|
1999-09-08 15:40:46 +00:00
|
|
|
addition to data storage, along with builtin history and line
|
1994-05-26 06:18:55 +00:00
|
|
|
editing capabilities. It incorporates many features to
|
|
|
|
aid interactive use and has the advantage that the interpretative
|
|
|
|
language is common to both interactive and non-interactive
|
|
|
|
use (shell scripts). That is, commands can be typed directly
|
1999-10-07 13:56:43 +00:00
|
|
|
to the running shell or can be put into a file,
|
|
|
|
which can be executed directly by the shell.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Invocation
|
1999-10-07 13:56:43 +00:00
|
|
|
.\"
|
|
|
|
.\" XXX This next sentence is incredibly confusing.
|
|
|
|
.\"
|
|
|
|
If no arguments are present and if the standard input of the shell
|
|
|
|
is connected to a terminal
|
|
|
|
(or if the
|
|
|
|
.Fl i
|
|
|
|
option is set),
|
|
|
|
the shell is considered an interactive shell. An interactive shell
|
1994-05-26 06:18:55 +00:00
|
|
|
generally prompts before each command and handles programming
|
|
|
|
and command errors differently (as described below).
|
|
|
|
When first starting, the shell inspects argument 0, and
|
1999-10-07 13:56:43 +00:00
|
|
|
if it begins with a dash
|
|
|
|
.Pq Li - ,
|
|
|
|
the shell is also considered a login shell.
|
|
|
|
This is normally done automatically by the system
|
1997-04-28 03:20:09 +00:00
|
|
|
when the user first logs in. A login shell first reads commands
|
|
|
|
from the files
|
|
|
|
.Pa /etc/profile
|
1999-10-07 13:56:43 +00:00
|
|
|
and then
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pa .profile
|
|
|
|
if they exist. If the environment variable
|
|
|
|
.Ev ENV
|
|
|
|
is set on entry to a shell, or is set in the
|
|
|
|
.Pa .profile
|
1999-10-07 13:56:43 +00:00
|
|
|
of a login shell, the shell then reads commands from the file named in
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ev ENV .
|
|
|
|
Therefore, a user should place commands that are to be executed only
|
|
|
|
at login time in the
|
|
|
|
.Pa .profile
|
|
|
|
file, and commands that are executed for every shell inside the
|
|
|
|
.Ev ENV
|
1999-09-14 11:46:04 +00:00
|
|
|
file.
|
1999-10-07 13:56:43 +00:00
|
|
|
The user can set the
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ev ENV
|
1999-10-07 13:56:43 +00:00
|
|
|
variable to some file by placing the following line in the file
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pa .profile
|
1999-10-07 13:56:43 +00:00
|
|
|
in the home directory,
|
1997-04-28 03:20:09 +00:00
|
|
|
substituting for
|
|
|
|
.Pa .shinit
|
1999-10-07 13:56:43 +00:00
|
|
|
the filename desired:
|
|
|
|
.Pp
|
|
|
|
.Dl ENV=$HOME/.shinit; export ENV
|
|
|
|
.Pp
|
|
|
|
The first non-option argument specified on the command line
|
|
|
|
will be treated as the
|
1994-05-26 06:18:55 +00:00
|
|
|
name of a file from which to read commands (a shell script), and
|
|
|
|
the remaining arguments are set as the positional parameters
|
|
|
|
of the shell ($1, $2, etc). Otherwise, the shell reads commands
|
|
|
|
from its standard input.
|
1997-05-07 11:42:00 +00:00
|
|
|
.Pp
|
|
|
|
Unlike older versions of
|
|
|
|
.Nm
|
|
|
|
the
|
|
|
|
.Ev ENV
|
|
|
|
script is only sourced on invocation of interactive shells. This
|
|
|
|
closes a well-known, and sometimes easily exploitable security
|
|
|
|
hole related to poorly thought out
|
|
|
|
.Ev ENV
|
|
|
|
scripts.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Argument List Processing
|
|
|
|
All of the single letter options to
|
|
|
|
.Nm
|
1999-10-07 13:56:43 +00:00
|
|
|
have a corresponding long name,
|
|
|
|
with the exception of
|
|
|
|
.Fl c
|
|
|
|
and
|
|
|
|
.Fl /+o .
|
|
|
|
These long names are provided next to the single letter options
|
|
|
|
in the descriptions below.
|
|
|
|
The long name for an option may be specified as an argument to the
|
|
|
|
.Fl /+o
|
|
|
|
option of
|
|
|
|
.Xr sh 1 .
|
|
|
|
Once the shell is running,
|
|
|
|
the long name for an option may be specified as an argument to the
|
|
|
|
.Fl /+o
|
|
|
|
option of the
|
1999-09-08 15:40:46 +00:00
|
|
|
.Ic set
|
1999-10-07 13:56:43 +00:00
|
|
|
builtin command
|
|
|
|
(described later in the section called
|
|
|
|
.Sx Builtin Commands ) .
|
|
|
|
Introducing an option with a dash
|
|
|
|
.Pq Li -
|
|
|
|
enables the option,
|
|
|
|
while using a plus
|
|
|
|
.Pq Li +
|
|
|
|
disables the option.
|
|
|
|
A
|
|
|
|
.Dq Li --
|
1998-07-30 04:47:56 +00:00
|
|
|
or plain
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li -
|
1998-07-30 04:47:56 +00:00
|
|
|
will stop option processing and will force the remaining
|
|
|
|
words on the command line to be treated as arguments.
|
1999-10-07 13:56:43 +00:00
|
|
|
The
|
|
|
|
.Fl /+o
|
|
|
|
and
|
|
|
|
.Fl c
|
|
|
|
options do not have long names.
|
|
|
|
They take arguments and are described after the single letter options.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It Fl a Li allexport
|
1999-10-07 13:56:43 +00:00
|
|
|
Flag variables for export when assignments are made to them.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl b Li notify
|
|
|
|
Enable asynchronous notification of background job
|
|
|
|
completion.
|
|
|
|
.Pq UNIMPLEMENTED
|
|
|
|
.It Fl C Li noclobber
|
1999-09-08 15:40:46 +00:00
|
|
|
Do not overwrite existing files with
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li > .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pq UNIMPLEMENTED
|
|
|
|
.It Fl E Li emacs
|
1999-09-08 15:40:46 +00:00
|
|
|
Enable the builtin
|
1997-04-28 03:20:09 +00:00
|
|
|
.Xr emacs 1
|
1999-10-07 13:56:43 +00:00
|
|
|
command line editor (disables the
|
1997-04-28 03:20:09 +00:00
|
|
|
.Fl V
|
1999-10-07 13:56:43 +00:00
|
|
|
option if it has been set).
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl e Li errexit
|
1999-10-07 13:56:43 +00:00
|
|
|
Exit immediately if any untested command fails in non-interactive mode.
|
1994-05-26 06:18:55 +00:00
|
|
|
The exit status of a command is considered to be
|
|
|
|
explicitly tested if the command is used to control
|
|
|
|
an if, elif, while, or until; or if the command is the left
|
1997-04-28 03:20:09 +00:00
|
|
|
hand operand of an
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li &&
|
1997-04-28 03:20:09 +00:00
|
|
|
or
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li ||
|
1997-04-28 03:20:09 +00:00
|
|
|
operator.
|
|
|
|
.It Fl f Li noglob
|
1994-05-26 06:18:55 +00:00
|
|
|
Disable pathname expansion.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl I Li ignoreeof
|
1999-10-07 13:56:43 +00:00
|
|
|
Ignore
|
|
|
|
.Dv EOF Ns ' Ns s
|
|
|
|
from input when in interactive mode.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl i Li interactive
|
|
|
|
Force the shell to behave interactively.
|
|
|
|
.It Fl m Li monitor
|
|
|
|
Turn on job control (set automatically when interactive).
|
|
|
|
.It Fl n Li noexec
|
1994-05-26 06:18:55 +00:00
|
|
|
If not interactive, read commands but do not
|
|
|
|
execute them. This is useful for checking the
|
|
|
|
syntax of shell scripts.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl p Li privileged
|
1996-10-29 03:12:51 +00:00
|
|
|
Turn on privileged mode. This mode is enabled on startup
|
|
|
|
if either the effective user or group id is not equal to the
|
|
|
|
real user or group id. Turning this mode off sets the
|
|
|
|
effective user and group ids to the real user and group ids.
|
1999-10-07 13:56:43 +00:00
|
|
|
When this mode is enabled for interactive shells, the file
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pa /etc/suid_profile
|
1999-10-07 13:56:43 +00:00
|
|
|
is sourced instead of
|
|
|
|
.Pa ~/.profile
|
1997-04-28 03:20:09 +00:00
|
|
|
after
|
|
|
|
.Pa /etc/profile
|
1999-10-07 13:56:43 +00:00
|
|
|
is sourced, and the contents of the
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ev ENV
|
1999-10-07 13:56:43 +00:00
|
|
|
variable are ignored.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl s Li stdin
|
|
|
|
Read commands from standard input (set automatically
|
|
|
|
if no file arguments are present). This option has
|
|
|
|
no effect when set after the shell has already started
|
1999-08-30 10:13:04 +00:00
|
|
|
running (i.e. when set with the
|
1999-09-08 15:40:46 +00:00
|
|
|
.Ic set
|
1999-08-30 10:13:04 +00:00
|
|
|
command).
|
1999-04-01 13:27:36 +00:00
|
|
|
.It Fl T Li asynctraps
|
1999-09-14 11:46:04 +00:00
|
|
|
When waiting for a child, execute traps immediately.
|
|
|
|
If this option is not set,
|
|
|
|
traps are executed after the child exits,
|
|
|
|
as specified in
|
1999-04-01 13:27:36 +00:00
|
|
|
.St -p1003.2
|
1999-08-30 10:13:04 +00:00
|
|
|
This nonstandard option is useful for putting guarding shells around
|
|
|
|
children that block signals. The surrounding shell may kill the child
|
|
|
|
or it may just return control to the tty and leave the child alone,
|
|
|
|
like this:
|
1999-04-01 13:27:36 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl u Li nounset
|
1994-05-26 06:18:55 +00:00
|
|
|
Write a message to standard error when attempting
|
|
|
|
to expand a variable that is not set, and if the
|
|
|
|
shell is not interactive, exit immediately.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pq UNIMPLEMENTED
|
|
|
|
.It Fl V Li vi
|
1999-09-08 15:40:46 +00:00
|
|
|
Enable the builtin
|
1997-04-28 03:20:09 +00:00
|
|
|
.Xr vi 1
|
1999-08-30 10:13:04 +00:00
|
|
|
command line editor (disables
|
1997-04-28 03:20:09 +00:00
|
|
|
.Fl E
|
|
|
|
if it has been set).
|
|
|
|
.It Fl v Li verbose
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell writes its input to standard error
|
|
|
|
as it is read. Useful for debugging.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Fl x Li xtrace
|
1999-10-07 13:56:43 +00:00
|
|
|
Write each command
|
|
|
|
(preceded by
|
|
|
|
.Dq Li +\ )
|
|
|
|
to standard error before it is executed.
|
|
|
|
Useful for debugging.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fl c
|
|
|
|
option may be used to pass its string argument to the shell
|
|
|
|
to be interpreted as input.
|
1997-09-13 17:40:00 +00:00
|
|
|
Keep in mind that this option only accepts a single string as its
|
|
|
|
argument, hence multi-word strings must be quoted.
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fl /+o
|
|
|
|
option takes as its only argument the long name of an option
|
|
|
|
to be enabled or disabled.
|
|
|
|
For example, the following two invocations of
|
|
|
|
.Nm
|
|
|
|
both enable the builtin
|
|
|
|
.Xr emacs 1
|
|
|
|
command line editor:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
set -E
|
|
|
|
set -o emacs
|
|
|
|
.Ed
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Lexical Structure
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell reads input in terms of lines from a file and breaks
|
|
|
|
it up into words at whitespace (blanks and tabs), and at
|
|
|
|
certain sequences of
|
1999-08-30 10:13:04 +00:00
|
|
|
characters called
|
|
|
|
.Dq operators ,
|
|
|
|
which are special to the shell.
|
1994-05-26 06:18:55 +00:00
|
|
|
There are two types of operators: control operators and
|
|
|
|
redirection operators (their meaning is discussed later).
|
1997-04-28 03:20:09 +00:00
|
|
|
The following is a list of valid operators:
|
|
|
|
.Bl -tag -width Ds
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Control operators:
|
|
|
|
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
|
|
|
|
.It Xo
|
|
|
|
.Li & Ta Xo
|
|
|
|
.Li && Ta Xo
|
|
|
|
.Li ( Ta Xo
|
|
|
|
.Li ) Ta Xo
|
|
|
|
.Li \en
|
|
|
|
.Xc Xc Xc Xc Xc
|
|
|
|
.It Xo
|
|
|
|
.Li ;; Ta Xo
|
|
|
|
.Li ; Ta Xo
|
|
|
|
.Li | Ta Xo
|
|
|
|
.Li ||
|
|
|
|
.Xc Xc Xc Xc
|
|
|
|
.El
|
|
|
|
.It Redirection operators:
|
2000-10-03 23:13:14 +00:00
|
|
|
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Xo
|
|
|
|
.Li < Ta Xo
|
|
|
|
.Li > Ta Xo
|
|
|
|
.Li << Ta Xo
|
2000-10-03 23:13:14 +00:00
|
|
|
.Li >> Ta Xo
|
|
|
|
.Li <>
|
|
|
|
.Xc Xc Xc Xc Xc
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Xo
|
|
|
|
.Li <& Ta Xo
|
|
|
|
.Li >& Ta Xo
|
|
|
|
.Li <<- Ta Xo
|
|
|
|
.Li >|
|
|
|
|
.Xc Xc Xc Xc
|
|
|
|
.El
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Ss Quoting
|
1994-05-26 06:18:55 +00:00
|
|
|
Quoting is used to remove the special meaning of certain characters
|
|
|
|
or words to the shell, such as operators, whitespace, or
|
|
|
|
keywords. There are three types of quoting: matched single quotes,
|
|
|
|
matched double quotes, and backslash.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It Single Quotes
|
1994-05-26 06:18:55 +00:00
|
|
|
Enclosing characters in single quotes preserves the literal
|
1996-12-14 06:20:03 +00:00
|
|
|
meaning of all the characters (except single quotes, making
|
|
|
|
it impossible to put single-quotes in a single-quoted string).
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Double Quotes
|
1994-05-26 06:18:55 +00:00
|
|
|
Enclosing characters within double quotes preserves the literal
|
1999-10-07 13:56:43 +00:00
|
|
|
meaning of all characters except dollarsign
|
|
|
|
.Pq Li $ ,
|
|
|
|
backquote
|
|
|
|
.Pq Li ` ,
|
|
|
|
and backslash
|
|
|
|
.Po Li \e\"
|
|
|
|
.Pc .
|
|
|
|
The backslash inside double quotes is historically weird.
|
|
|
|
It remains literal unless it precedes the following characters,
|
|
|
|
which it serves to quote:
|
|
|
|
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
|
|
|
|
.It Xo
|
|
|
|
.Li $ Ta Xo
|
|
|
|
.Li ` Ta Xo
|
|
|
|
.Li \&" Ta Xo
|
|
|
|
.Li \e\ Ta Xo
|
|
|
|
.Li \en
|
|
|
|
.Xc Xc Xc Xc Xc
|
|
|
|
.El
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Backslash
|
|
|
|
A backslash preserves the literal meaning of the following
|
1999-10-07 13:56:43 +00:00
|
|
|
character, with the exception of the newline character
|
|
|
|
.Pq Li \en .
|
|
|
|
A backslash preceding a newline is treated as a line continuation.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Ss Reserved Words
|
1994-05-26 06:18:55 +00:00
|
|
|
Reserved words are words that have special meaning to the
|
|
|
|
shell and are recognized at the beginning of a line and
|
|
|
|
after a control operator. The following are reserved words:
|
1999-10-07 13:56:43 +00:00
|
|
|
.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
|
|
|
|
.It Xo
|
2000-12-18 14:48:17 +00:00
|
|
|
.Li \&! Ta Xo
|
1999-10-07 13:56:43 +00:00
|
|
|
.Li { Ta Xo
|
|
|
|
.Li } Ta Xo
|
|
|
|
.Ic case Ta Xo
|
|
|
|
.Ic do
|
|
|
|
.Xc Xc Xc Xc Xc
|
|
|
|
.It Xo
|
|
|
|
.Ic done Ta Xo
|
|
|
|
.Ic elif Ta Xo
|
|
|
|
.Ic else Ta Xo
|
|
|
|
.Ic esac Ta Xo
|
|
|
|
.Ic fi
|
|
|
|
.Xc Xc Xc Xc Xc
|
|
|
|
.It Xo
|
|
|
|
.Ic for Ta Xo
|
|
|
|
.Ic if Ta Xo
|
|
|
|
.Ic then Ta Xo
|
|
|
|
.Ic until Ta Xo
|
|
|
|
.Ic while
|
|
|
|
.Xc Xc Xc Xc Xc
|
|
|
|
.El
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Aliases
|
|
|
|
An alias is a name and corresponding value set using the
|
1999-09-08 15:40:46 +00:00
|
|
|
.Ic alias
|
1994-05-26 06:18:55 +00:00
|
|
|
builtin command. Whenever a reserved word may occur (see above),
|
|
|
|
and after checking for reserved words, the shell
|
1999-09-14 11:46:04 +00:00
|
|
|
checks the word to see if it matches an alias.
|
|
|
|
If it does, it replaces it in the input stream with its value.
|
|
|
|
For example, if there is an alias called
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li lf
|
1999-08-30 10:13:04 +00:00
|
|
|
with the value
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li ls -F ,
|
1994-05-26 06:18:55 +00:00
|
|
|
then the input
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
1999-10-07 13:56:43 +00:00
|
|
|
lf foobar
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
would become
|
|
|
|
.Bd -literal -offset indent
|
1999-10-07 13:56:43 +00:00
|
|
|
ls -F foobar
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Aliases provide a convenient way for naive users to
|
|
|
|
create shorthands for commands without having to learn how
|
|
|
|
to create functions with arguments. They can also be
|
|
|
|
used to create lexically obscure code. This use is discouraged.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Commands
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell interprets the words it reads according to a
|
|
|
|
language, the specification of which is outside the scope
|
1997-04-28 03:20:09 +00:00
|
|
|
of this man page (refer to the BNF in the
|
|
|
|
.St -p1003.2
|
1994-05-26 06:18:55 +00:00
|
|
|
document). Essentially though, a line is read and if
|
|
|
|
the first word of the line (or after a control operator)
|
|
|
|
is not a reserved word, then the shell has recognized a
|
|
|
|
simple command. Otherwise, a complex command or some
|
|
|
|
other special construct may have been recognized.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Simple Commands
|
1994-05-26 06:18:55 +00:00
|
|
|
If a simple command has been recognized, the shell performs
|
|
|
|
the following actions:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -enum
|
|
|
|
.It
|
1999-08-30 10:13:04 +00:00
|
|
|
Leading words of the form
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li name=value
|
1999-08-30 10:13:04 +00:00
|
|
|
are stripped off and assigned to the environment of
|
1994-05-26 06:18:55 +00:00
|
|
|
the simple command. Redirection operators and
|
|
|
|
their arguments (as described below) are stripped
|
|
|
|
off and saved for processing.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
|
|
|
The remaining words are expanded as described in
|
1999-08-30 10:13:04 +00:00
|
|
|
the section called
|
|
|
|
.Sx Word Expansions ,
|
|
|
|
and the first remaining word is considered the command
|
1994-05-26 06:18:55 +00:00
|
|
|
name and the command is located. The remaining
|
|
|
|
words are considered the arguments of the command.
|
1999-08-30 10:13:04 +00:00
|
|
|
If no command name resulted, then the
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li name=value
|
1994-05-26 06:18:55 +00:00
|
|
|
variable assignments recognized in 1) affect the
|
|
|
|
current shell.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
|
|
|
Redirections are performed as described in
|
1994-05-26 06:18:55 +00:00
|
|
|
the next section.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Ss Redirections
|
1994-05-26 06:18:55 +00:00
|
|
|
Redirections are used to change where a command reads its input
|
|
|
|
or sends its output. In general, redirections open, close, or
|
|
|
|
duplicate an existing reference to a file. The overall format
|
|
|
|
used for redirection is:
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pp
|
1997-04-28 03:20:09 +00:00
|
|
|
.Dl [n] redir-op file
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Ql redir-op
|
|
|
|
is one of the redirection operators mentioned
|
1997-04-28 03:20:09 +00:00
|
|
|
previously. The following gives some examples of how these
|
1999-10-07 13:56:43 +00:00
|
|
|
operators can be used.
|
|
|
|
Note that stdin and stdout are commonly used abbreviations
|
|
|
|
for standard input and standard output respectively.
|
|
|
|
.Bl -tag -width "1234567890XX" -offset indent
|
|
|
|
.It Li [n]> file
|
|
|
|
redirect stdout (or file descriptor n) to file
|
|
|
|
.It Li [n]>| file
|
|
|
|
same as above, but override the
|
|
|
|
.Fl C
|
|
|
|
option
|
|
|
|
.It Li [n]>> file
|
|
|
|
append stdout (or file descriptor n) to file
|
|
|
|
.It Li [n]< file
|
|
|
|
redirect stdin (or file descriptor n) from file
|
2000-10-03 23:13:14 +00:00
|
|
|
.It Li [n]<> file
|
|
|
|
redirect stdin (or file descriptor n) to and from file
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Li [n1]<&n2
|
|
|
|
duplicate stdin (or file descriptor n1) from file descriptor n2
|
|
|
|
.It Li [n]<&-
|
|
|
|
close stdin (or file descriptor n)
|
|
|
|
.It Li [n1]>&n2
|
|
|
|
duplicate stdout (or file descriptor n1) to file descriptor n2
|
|
|
|
.It Li [n]>&-
|
|
|
|
close stdout (or file descriptor n)
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
1999-08-30 10:13:04 +00:00
|
|
|
The following redirection is often called a
|
|
|
|
.Dq here-document .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
[n]<< delimiter
|
1999-10-07 13:56:43 +00:00
|
|
|
here-doc-text
|
|
|
|
...
|
1997-04-28 03:20:09 +00:00
|
|
|
delimiter
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
All the text on successive lines up to the delimiter is
|
|
|
|
saved away and made available to the command on standard
|
|
|
|
input, or file descriptor n if it is specified. If the delimiter
|
|
|
|
as specified on the initial line is quoted, then the here-doc-text
|
|
|
|
is treated literally, otherwise the text is subjected to
|
|
|
|
parameter expansion, command substitution, and arithmetic
|
1999-08-30 10:13:04 +00:00
|
|
|
expansion (as described in the section on
|
|
|
|
.Sx Word Expansions ) .
|
|
|
|
If the operator is
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li <<-
|
1999-08-30 10:13:04 +00:00
|
|
|
instead of
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li << ,
|
1999-08-30 10:13:04 +00:00
|
|
|
then leading tabs
|
1994-05-26 06:18:55 +00:00
|
|
|
in the here-doc-text are stripped.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Search and Execution
|
1996-12-14 06:20:03 +00:00
|
|
|
There are three types of commands: shell functions,
|
1999-10-07 13:56:43 +00:00
|
|
|
builtin commands, and normal programs.
|
|
|
|
The command is searched for (by name) in that order.
|
|
|
|
The three types of commands are all executed in a different way.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1996-12-14 06:20:03 +00:00
|
|
|
When a shell function is executed, all of the shell positional
|
|
|
|
parameters (except $0, which remains unchanged) are
|
1994-05-26 06:18:55 +00:00
|
|
|
set to the arguments of the shell function.
|
|
|
|
The variables which are explicitly placed in the environment of
|
|
|
|
the command (by placing assignments to them before the
|
|
|
|
function name) are made local to the function and are set
|
1999-09-14 11:46:04 +00:00
|
|
|
to the values given.
|
|
|
|
Then the command given in the function definition is executed.
|
|
|
|
The positional parameters are restored to their original values
|
|
|
|
when the command completes.
|
1996-12-14 06:20:03 +00:00
|
|
|
This all occurs within the current shell.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-09-08 15:40:46 +00:00
|
|
|
Shell builtin commands are executed internally to the shell, without
|
1996-12-14 06:20:03 +00:00
|
|
|
spawning a new process.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-09-08 15:40:46 +00:00
|
|
|
Otherwise, if the command name does not match a function
|
|
|
|
or builtin command, the command is searched for as a normal
|
1994-05-26 06:18:55 +00:00
|
|
|
program in the filesystem (as described in the next section).
|
|
|
|
When a normal program is executed, the shell runs the program,
|
1999-09-14 11:46:04 +00:00
|
|
|
passing the arguments and the environment to the program.
|
|
|
|
If the program is not a normal executable file
|
1999-08-30 10:13:04 +00:00
|
|
|
(i.e. if it does not begin with the
|
|
|
|
.Qq magic number
|
|
|
|
whose
|
1997-04-28 03:20:09 +00:00
|
|
|
.Tn ASCII
|
1999-08-30 10:13:04 +00:00
|
|
|
representation is
|
|
|
|
.Qq #! ,
|
1999-10-07 13:56:43 +00:00
|
|
|
resulting in an
|
2000-11-22 15:55:01 +00:00
|
|
|
.Er ENOEXEC
|
1999-10-07 13:56:43 +00:00
|
|
|
return value from
|
|
|
|
.Xr execve 2 )
|
1999-09-14 11:46:04 +00:00
|
|
|
the shell will interpret the program in a subshell.
|
|
|
|
The child shell will reinitialize itself in this case,
|
|
|
|
so that the effect will be
|
|
|
|
as if a new shell had been invoked to handle the ad-hoc shell script,
|
|
|
|
except that the location of hashed commands located in
|
1994-05-26 06:18:55 +00:00
|
|
|
the parent shell will be remembered by the child.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1996-12-14 06:20:03 +00:00
|
|
|
Note that previous versions of this document
|
|
|
|
and the source code itself misleadingly and sporadically
|
|
|
|
refer to a shell script without a magic number
|
1999-08-30 10:13:04 +00:00
|
|
|
as a
|
|
|
|
.Qq shell procedure .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Path Search
|
1994-05-26 06:18:55 +00:00
|
|
|
When locating a command, the shell first looks to see if
|
|
|
|
it has a shell function by that name. Then it looks for a
|
1996-12-14 06:20:03 +00:00
|
|
|
builtin command by that name. If a builtin command is not found,
|
|
|
|
one of two things happen:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -enum
|
|
|
|
.It
|
|
|
|
Command names containing a slash are simply executed without
|
1996-12-14 06:20:03 +00:00
|
|
|
performing any searches.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
|
|
|
The shell searches each entry in
|
|
|
|
.Ev PATH
|
|
|
|
in turn for the command. The value of the
|
|
|
|
.Ev PATH
|
|
|
|
variable should be a series of
|
1994-05-26 06:18:55 +00:00
|
|
|
entries separated by colons. Each entry consists of a
|
|
|
|
directory name.
|
|
|
|
The current directory
|
1996-12-14 06:20:03 +00:00
|
|
|
may be indicated implicitly by an empty directory name,
|
|
|
|
or explicitly by a single period.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Ss Command Exit Status
|
1994-05-26 06:18:55 +00:00
|
|
|
Each command has an exit status that can influence the behavior
|
|
|
|
of other shell commands. The paradigm is that a command exits
|
|
|
|
with zero for normal or success, and non-zero for failure,
|
|
|
|
error, or a false indication. The man page for each command
|
|
|
|
should indicate the various exit codes and what they mean.
|
|
|
|
Additionally, the builtin commands return exit codes, as does
|
1996-12-14 06:20:03 +00:00
|
|
|
an executed shell function.
|
1997-11-12 04:32:50 +00:00
|
|
|
.Pp
|
|
|
|
If a command is terminated by a signal, its exit status is 128 plus
|
|
|
|
the signal number. Signal numbers are defined in the header file
|
|
|
|
.Aq Pa sys/signal.h .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Complex Commands
|
1994-05-26 06:18:55 +00:00
|
|
|
Complex commands are combinations of simple commands
|
|
|
|
with control operators or reserved words, together creating a larger complex
|
|
|
|
command. More generally, a command is one of the following:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -item -offset indent
|
|
|
|
.It
|
|
|
|
simple command
|
|
|
|
.It
|
|
|
|
pipeline
|
|
|
|
.It
|
|
|
|
list or compound-list
|
|
|
|
.It
|
|
|
|
compound command
|
|
|
|
.It
|
|
|
|
function definition
|
|
|
|
.El
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Unless otherwise stated, the exit status of a command is
|
|
|
|
that of the last simple command executed by the command.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Pipelines
|
1994-05-26 06:18:55 +00:00
|
|
|
A pipeline is a sequence of one or more commands separated
|
|
|
|
by the control operator |. The standard output of all but
|
|
|
|
the last command is connected to the standard input
|
1996-12-14 06:20:03 +00:00
|
|
|
of the next command. The standard output of the last
|
|
|
|
command is inherited from the shell, as usual.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The format for a pipeline is:
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pp
|
|
|
|
.Dl [!] command1 [ | command2 ...]
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The standard output of command1 is connected to the standard
|
1997-04-28 03:20:09 +00:00
|
|
|
input of command2. The standard input, standard output, or
|
1994-05-26 06:18:55 +00:00
|
|
|
both of a command is considered to be assigned by the
|
|
|
|
pipeline before any redirection specified by redirection
|
|
|
|
operators that are part of the command.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
If the pipeline is not in the background (discussed later),
|
|
|
|
the shell waits for all commands to complete.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
If the reserved word ! does not precede the pipeline, the
|
|
|
|
exit status is the exit status of the last command specified
|
|
|
|
in the pipeline. Otherwise, the exit status is the logical
|
|
|
|
NOT of the exit status of the last command. That is, if
|
|
|
|
the last command returns zero, the exit status is 1; if
|
|
|
|
the last command returns greater than zero, the exit status
|
|
|
|
is zero.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Because pipeline assignment of standard input or standard
|
|
|
|
output or both takes place before redirection, it can be
|
|
|
|
modified by redirection. For example:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dl $ command1 2>&1 | command2
|
|
|
|
.Pp
|
|
|
|
sends both the standard output and standard error of
|
|
|
|
.Ql command1
|
|
|
|
to the standard input of
|
|
|
|
.Ql command2 .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
A
|
2000-12-15 17:37:31 +00:00
|
|
|
.Dq Li \&;
|
1999-10-07 13:56:43 +00:00
|
|
|
or newline terminator causes the preceding
|
1999-11-18 08:13:21 +00:00
|
|
|
AND-OR-list
|
|
|
|
(described below in the section called
|
|
|
|
.Sx Short-Circuit List Operators )
|
|
|
|
to be executed sequentially;
|
|
|
|
an
|
|
|
|
.Dq Li &
|
|
|
|
causes asynchronous execution of the preceding AND-OR-list.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
Note that unlike some other shells,
|
|
|
|
.Nm
|
|
|
|
executes each process in the pipeline as a child of the
|
|
|
|
.Nm
|
|
|
|
process.
|
|
|
|
Shell builtin commands are the exception to this rule.
|
|
|
|
They are executed in the current shell, although they do not affect its
|
|
|
|
environment when used in pipelines.
|
|
|
|
.Ss Background Commands (&)
|
1994-05-26 06:18:55 +00:00
|
|
|
If a command is terminated by the control operator ampersand
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pq Li & ,
|
|
|
|
the shell executes the command asynchronously;
|
|
|
|
the shell does not wait for the command to finish
|
|
|
|
before executing the next command.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The format for running a command in background is:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
1994-05-26 06:18:55 +00:00
|
|
|
command1 & [command2 & ...]
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
If the shell is not interactive, the standard input of an
|
|
|
|
asynchronous command is set to /dev/null.
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ss Lists (Generally Speaking)
|
1994-05-26 06:18:55 +00:00
|
|
|
A list is a sequence of zero or more commands separated by
|
|
|
|
newlines, semicolons, or ampersands,
|
|
|
|
and optionally terminated by one of these three characters.
|
|
|
|
The commands in a
|
|
|
|
list are executed in the order they are written.
|
|
|
|
If command is followed by an ampersand, the shell starts the
|
|
|
|
command and immediately proceed onto the next command;
|
|
|
|
otherwise it waits for the command to terminate before
|
|
|
|
proceeding to the next one.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Short-Circuit List Operators
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li &&
|
1999-08-30 10:13:04 +00:00
|
|
|
and
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li ||
|
1999-08-30 10:13:04 +00:00
|
|
|
are AND-OR list operators.
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li &&
|
1999-08-30 10:13:04 +00:00
|
|
|
executes the first command, and then executes the second command
|
|
|
|
if the exit status of the first command is zero.
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li ||
|
1999-04-19 18:48:26 +00:00
|
|
|
is similar, but executes the second command if the exit
|
1999-08-30 10:13:04 +00:00
|
|
|
status of the first command is nonzero.
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li &&
|
1999-08-30 10:13:04 +00:00
|
|
|
and
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li ||
|
1994-05-26 06:18:55 +00:00
|
|
|
both have the same priority.
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ss Flow-Control Constructs (if, while, for, case)
|
|
|
|
The syntax of the
|
|
|
|
.Ic if
|
|
|
|
command is:
|
|
|
|
.\"
|
|
|
|
.\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed .
|
|
|
|
.\"
|
|
|
|
.Dl Ic if Ar list
|
|
|
|
.Dl Ic then Ar list
|
|
|
|
.Dl [ Ic elif Ar list
|
|
|
|
.Dl Ic then Ar list ] ...
|
|
|
|
.Dl [ Ic else Ar list ]
|
|
|
|
.Dl Ic fi
|
|
|
|
.Pp
|
|
|
|
The syntax of the
|
|
|
|
.Ic while
|
|
|
|
command is:
|
|
|
|
.Dl Ic while Ar list
|
|
|
|
.Dl Ic do Ar list
|
|
|
|
.Dl Ic done
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1996-12-14 06:20:03 +00:00
|
|
|
The two lists are executed repeatedly while the exit status of the
|
1999-10-07 13:56:43 +00:00
|
|
|
first list is zero.
|
|
|
|
The
|
|
|
|
.Ic until
|
|
|
|
command is similar, but has the word
|
|
|
|
.Ic until
|
|
|
|
in place of
|
|
|
|
.Ic while ,
|
|
|
|
which causes it to
|
1994-05-26 06:18:55 +00:00
|
|
|
repeat until the exit status of the first list is zero.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The syntax of the
|
|
|
|
.Ic for
|
|
|
|
command is:
|
|
|
|
.Dl Ic for Ar variable Ic in Ar word ...
|
|
|
|
.Dl Ic do Ar list
|
|
|
|
.Dl Ic done
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The words are expanded, and then the list is executed
|
1999-10-07 13:56:43 +00:00
|
|
|
repeatedly with the variable set to each word in turn.
|
|
|
|
The
|
|
|
|
.Ic do
|
1999-08-30 10:13:04 +00:00
|
|
|
and
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ic done
|
|
|
|
commands may be replaced with
|
|
|
|
.Dq Li {
|
|
|
|
and
|
|
|
|
.Dq Li } .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The syntax of the
|
|
|
|
.Ic break
|
|
|
|
and
|
|
|
|
.Ic continue
|
|
|
|
commands is:
|
|
|
|
.Dl Ic break Op Ar num
|
|
|
|
.Dl Ic continue Op Ar num
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Ic break
|
|
|
|
command terminates the
|
|
|
|
.Ar num
|
|
|
|
innermost
|
|
|
|
.Ic for
|
|
|
|
or
|
|
|
|
.Ic while
|
|
|
|
loops.
|
|
|
|
The
|
|
|
|
.Ic continue
|
|
|
|
command continues with the next iteration of the innermost loop.
|
1996-12-14 06:20:03 +00:00
|
|
|
These are implemented as builtin commands.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The syntax of the
|
|
|
|
.Ic case
|
|
|
|
command is
|
|
|
|
.Dl Ic case Ar word Ic in
|
|
|
|
.Dl pattern) list ;;
|
|
|
|
.Dl ...
|
|
|
|
.Dl Ic esac
|
|
|
|
.Pp
|
|
|
|
The pattern can actually be one or more patterns
|
|
|
|
(see
|
|
|
|
.Sx Shell Patterns
|
|
|
|
described later),
|
|
|
|
separated by
|
1999-12-05 12:13:54 +00:00
|
|
|
.Dq Li \&|
|
1999-08-30 10:13:04 +00:00
|
|
|
characters.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Grouping Commands Together
|
1994-05-26 06:18:55 +00:00
|
|
|
Commands may be grouped by writing either
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
(list)
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
or
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
{ list; }
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The first form executes the commands in a subshell.
|
|
|
|
Note that builtin commands thus executed do not affect the current shell.
|
|
|
|
The second form does not fork another shell,
|
|
|
|
so it is slightly more efficient.
|
|
|
|
Grouping commands together this way allows the user to
|
1996-12-14 06:20:03 +00:00
|
|
|
redirect their output as though they were one program:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
1998-01-02 19:23:05 +00:00
|
|
|
{ echo -n "hello"; echo " world"; } > greeting
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Ss Functions
|
1994-05-26 06:18:55 +00:00
|
|
|
The syntax of a function definition is
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
name ( ) command
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
A function definition is an executable statement; when
|
|
|
|
executed it installs a function named name and returns an
|
1997-04-28 03:20:09 +00:00
|
|
|
exit status of zero. The command is normally a list
|
1999-08-30 10:13:04 +00:00
|
|
|
enclosed between
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li {
|
1999-08-30 10:13:04 +00:00
|
|
|
and
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li } .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Variables may be declared to be local to a function by
|
1999-10-07 13:56:43 +00:00
|
|
|
using the
|
|
|
|
.Ic local
|
|
|
|
command.
|
|
|
|
This should appear as the first statement of a function,
|
|
|
|
and the syntax is:
|
|
|
|
.Bd -ragged -offset indent
|
|
|
|
.Ic local
|
2000-12-18 14:48:17 +00:00
|
|
|
.Op Ar variable ...\&
|
1999-10-07 13:56:43 +00:00
|
|
|
.Op Ar -
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The
|
|
|
|
.Ic local
|
|
|
|
command is implemented as a builtin command.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
When a variable is made local, it inherits the initial
|
|
|
|
value and exported and readonly flags from the variable
|
|
|
|
with the same name in the surrounding scope, if there is
|
1997-04-28 03:20:09 +00:00
|
|
|
one. Otherwise, the variable is initially unset. The shell
|
1999-11-18 08:13:21 +00:00
|
|
|
uses dynamic scoping, so that if the variable
|
|
|
|
.Em x
|
|
|
|
is made local to function
|
|
|
|
.Em f ,
|
|
|
|
which then calls function
|
|
|
|
.Em g ,
|
|
|
|
references to the variable
|
|
|
|
.Em x
|
|
|
|
made inside
|
|
|
|
.Em g
|
|
|
|
will refer to the variable
|
|
|
|
.Em x
|
|
|
|
declared inside
|
|
|
|
.Em f ,
|
|
|
|
not to the global variable named
|
|
|
|
.Em x .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The only special parameter than can be made local is
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li - .
|
1999-08-30 10:13:04 +00:00
|
|
|
Making
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li -
|
1999-08-30 10:13:04 +00:00
|
|
|
local causes any shell options that are
|
1994-05-26 06:18:55 +00:00
|
|
|
changed via the set command inside the function to be
|
|
|
|
restored to their original values when the function
|
|
|
|
returns.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The syntax of the
|
|
|
|
.Ic return
|
|
|
|
command is
|
|
|
|
.Bd -ragged -offset indent
|
|
|
|
.Ic return
|
|
|
|
.Op Ar exitstatus
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
It terminates the currently executing function.
|
|
|
|
The
|
|
|
|
.Ic return
|
|
|
|
command is implemented as a builtin command.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Variables and Parameters
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell maintains a set of parameters. A parameter
|
1997-04-28 03:20:09 +00:00
|
|
|
denoted by a name is called a variable. When starting up,
|
1994-05-26 06:18:55 +00:00
|
|
|
the shell turns all the environment variables into shell
|
|
|
|
variables. New variables can be set using the form
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
name=value
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Variables set by the user must have a name consisting solely
|
1999-10-07 13:56:43 +00:00
|
|
|
of alphabetics, numerics, and underscores.
|
|
|
|
The first letter of a variable name must not be numeric.
|
|
|
|
A parameter can also be denoted by a number
|
1994-05-26 06:18:55 +00:00
|
|
|
or a special character as explained below.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Positional Parameters
|
1999-10-07 13:56:43 +00:00
|
|
|
A positional parameter is a parameter denoted by a number greater than zero.
|
1999-08-30 10:13:04 +00:00
|
|
|
The shell sets these initially to the values of its command line
|
1997-04-28 03:20:09 +00:00
|
|
|
arguments that follow the name of the shell script. The
|
1999-09-08 15:40:46 +00:00
|
|
|
.Ic set
|
|
|
|
builtin command can also be used to set or reset them.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Special Parameters
|
1994-05-26 06:18:55 +00:00
|
|
|
A special parameter is a parameter denoted by one of the following
|
|
|
|
special characters. The value of the parameter is listed
|
|
|
|
next to its character.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -hang
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Li *
|
1994-05-26 06:18:55 +00:00
|
|
|
Expands to the positional parameters, starting from one. When
|
|
|
|
the expansion occurs within a double-quoted string
|
|
|
|
it expands to a single field with the value of each parameter
|
1999-10-07 13:56:43 +00:00
|
|
|
separated by the first character of the
|
|
|
|
.Ev IFS
|
|
|
|
variable,
|
|
|
|
or by a
|
|
|
|
.Aq space
|
|
|
|
if
|
|
|
|
.Ev IFS
|
|
|
|
is unset.
|
|
|
|
.It Li @
|
1994-05-26 06:18:55 +00:00
|
|
|
Expands to the positional parameters, starting from one. When
|
|
|
|
the expansion occurs within double-quotes, each positional
|
|
|
|
parameter expands as a separate argument.
|
|
|
|
If there are no positional parameters, the
|
1999-11-18 08:13:21 +00:00
|
|
|
expansion of
|
|
|
|
.Li @
|
|
|
|
generates zero arguments, even when
|
|
|
|
.Li @
|
|
|
|
is double-quoted. What this basically means, for example, is
|
1999-08-30 10:13:04 +00:00
|
|
|
if $1 is
|
|
|
|
.Dq abc
|
|
|
|
and $2 is
|
|
|
|
.Dq def ghi ,
|
|
|
|
then
|
1999-11-18 08:13:21 +00:00
|
|
|
.Qq Li $@
|
1999-08-30 10:13:04 +00:00
|
|
|
expands to
|
1994-05-26 06:18:55 +00:00
|
|
|
the two arguments:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
1994-05-26 06:18:55 +00:00
|
|
|
"abc" "def ghi"
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Li #
|
1994-05-26 06:18:55 +00:00
|
|
|
Expands to the number of positional parameters.
|
2000-12-18 14:48:17 +00:00
|
|
|
.It Li \&?
|
1994-05-26 06:18:55 +00:00
|
|
|
Expands to the exit status of the most recent pipeline.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Li -
|
1997-04-28 03:20:09 +00:00
|
|
|
(hyphen) Expands to the current option flags (the single-letter
|
1994-05-26 06:18:55 +00:00
|
|
|
option names concatenated into a string) as specified on
|
|
|
|
invocation, by the set builtin command, or implicitly
|
|
|
|
by the shell.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Li $
|
1994-05-26 06:18:55 +00:00
|
|
|
Expands to the process ID of the invoked shell. A subshell
|
|
|
|
retains the same value of $ as its parent.
|
2000-12-18 14:48:17 +00:00
|
|
|
.It Li \&!
|
1994-05-26 06:18:55 +00:00
|
|
|
Expands to the process ID of the most recent background
|
|
|
|
command executed from the current shell. For a
|
|
|
|
pipeline, the process ID is that of the last command in the
|
|
|
|
pipeline.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Li 0
|
1997-04-28 03:20:09 +00:00
|
|
|
(zero) Expands to the name of the shell or shell script.
|
|
|
|
.El
|
|
|
|
.Ss Word Expansions
|
1994-05-26 06:18:55 +00:00
|
|
|
This clause describes the various expansions that are
|
|
|
|
performed on words. Not all expansions are performed on
|
|
|
|
every word, as explained later.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Tilde expansions, parameter expansions, command substitutions,
|
|
|
|
arithmetic expansions, and quote removals that occur within
|
|
|
|
a single word expand to a single field. It is only field
|
|
|
|
splitting or pathname expansion that can create multiple
|
1999-09-14 11:46:04 +00:00
|
|
|
fields from a single word.
|
|
|
|
The single exception to this rule is
|
1999-11-18 08:13:21 +00:00
|
|
|
the expansion of the special parameter
|
|
|
|
.Li @
|
|
|
|
within double-quotes,
|
1999-09-14 11:46:04 +00:00
|
|
|
as was described above.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The order of word expansion is:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -enum
|
|
|
|
.It
|
|
|
|
Tilde Expansion, Parameter Expansion, Command Substitution,
|
1994-05-26 06:18:55 +00:00
|
|
|
Arithmetic Expansion (these all occur at the same time).
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
1999-10-07 13:56:43 +00:00
|
|
|
Field Splitting is performed on fields generated by step (1)
|
|
|
|
unless the
|
|
|
|
.Ev IFS
|
|
|
|
variable is null.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
1999-10-07 13:56:43 +00:00
|
|
|
Pathname Expansion (unless the
|
|
|
|
.Fl f
|
|
|
|
option is in effect).
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
|
|
|
Quote Removal.
|
|
|
|
.El
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The
|
|
|
|
.Dq Li $
|
|
|
|
character is used to introduce parameter expansion, command
|
1994-05-26 06:18:55 +00:00
|
|
|
substitution, or arithmetic evaluation.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Tilde Expansion (substituting a user's home directory)
|
1999-10-07 13:56:43 +00:00
|
|
|
A word beginning with an unquoted tilde character
|
|
|
|
.Pq Li ~
|
|
|
|
is
|
|
|
|
subjected to tilde expansion.
|
|
|
|
All the characters up to a slash
|
|
|
|
.Pq Li /
|
|
|
|
or the end of the word are treated as a username
|
1994-05-26 06:18:55 +00:00
|
|
|
and are replaced with the user's home directory. If the
|
|
|
|
username is missing (as in ~/foobar), the tilde is replaced
|
|
|
|
with the value of the HOME variable (the current user's
|
|
|
|
home directory).
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Parameter Expansion
|
1994-05-26 06:18:55 +00:00
|
|
|
The format for parameter expansion is as follows:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
${expression}
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
where expression consists of all characters until the matching
|
|
|
|
.Dq Li } .
|
|
|
|
Any
|
|
|
|
.Dq Li }
|
1994-05-26 06:18:55 +00:00
|
|
|
escaped by a backslash or within a quoted string, and characters in
|
|
|
|
embedded arithmetic expansions, command substitutions, and variable
|
1999-10-07 13:56:43 +00:00
|
|
|
expansions, are not examined in determining the matching
|
|
|
|
.Dq Li } .
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The simplest form for parameter expansion is:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
${parameter}
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The value, if any, of parameter is substituted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The parameter name or symbol can be enclosed in braces, which are
|
|
|
|
optional except for positional parameters with more than one digit or
|
|
|
|
when parameter is followed by a character that could be interpreted as
|
|
|
|
part of the name.
|
1997-04-28 03:20:09 +00:00
|
|
|
If a parameter expansion occurs inside double-quotes:
|
|
|
|
.Bl -enum
|
|
|
|
.It
|
|
|
|
Pathname expansion is not performed on the results of the
|
1994-05-26 06:18:55 +00:00
|
|
|
expansion.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
|
|
|
Field splitting is not performed on the results of the
|
1999-11-18 08:13:21 +00:00
|
|
|
expansion, with the exception of the special parameter
|
|
|
|
.Li @ .
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
In addition, a parameter expansion can be modified by using one of the
|
|
|
|
following formats.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It Li ${parameter:-word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Use Default Values. If parameter is unset or
|
|
|
|
null, the expansion of word is
|
|
|
|
substituted; otherwise, the value of
|
|
|
|
parameter is substituted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter:=word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Assign Default Values. If parameter is unset
|
|
|
|
or null, the expansion of word is
|
|
|
|
assigned to parameter. In all cases, the
|
|
|
|
final value of parameter is
|
|
|
|
substituted. Only variables, not positional
|
|
|
|
parameters or special parameters, can be
|
|
|
|
assigned in this way.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter:?[word]}
|
1994-05-26 06:18:55 +00:00
|
|
|
Indicate Error if Null or Unset. If
|
|
|
|
parameter is unset or null, the expansion of
|
|
|
|
word (or a message indicating it is unset if
|
|
|
|
word is omitted) is written to standard
|
|
|
|
error and the shell exits with a nonzero
|
1999-09-14 11:46:04 +00:00
|
|
|
exit status.
|
|
|
|
Otherwise, the value of
|
1994-05-26 06:18:55 +00:00
|
|
|
parameter is substituted. An
|
|
|
|
interactive shell need not exit.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter:+word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Use Alternate Value. If parameter is unset
|
|
|
|
or null, null is substituted;
|
|
|
|
otherwise, the expansion of word is
|
|
|
|
substituted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
In the parameter expansions shown previously, use of the colon in the
|
|
|
|
format results in a test for a parameter that is unset or null; omission
|
|
|
|
of the colon results in a test for a parameter that is only unset.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${#parameter}
|
1994-05-26 06:18:55 +00:00
|
|
|
String Length. The length in characters of
|
|
|
|
the value of parameter.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The following four varieties of parameter expansion provide for substring
|
1999-10-07 13:56:43 +00:00
|
|
|
processing.
|
|
|
|
In each case, pattern matching notation
|
|
|
|
(see
|
|
|
|
.Sx Shell Patterns ) ,
|
|
|
|
rather than regular expression notation,
|
|
|
|
is used to evaluate the patterns.
|
1999-11-18 08:13:21 +00:00
|
|
|
If parameter is one of the special parameters
|
|
|
|
.Li *
|
1999-10-07 13:56:43 +00:00
|
|
|
or
|
1999-11-18 08:13:21 +00:00
|
|
|
.Li @ ,
|
1999-10-07 13:56:43 +00:00
|
|
|
the result of the expansion is unspecified.
|
1994-05-26 06:18:55 +00:00
|
|
|
Enclosing the full parameter expansion string in double-quotes does not
|
|
|
|
cause the following four varieties of pattern characters to be quoted,
|
|
|
|
whereas quoting characters within the braces has this effect.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter%word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Remove Smallest Suffix Pattern. The word
|
|
|
|
is expanded to produce a pattern. The
|
|
|
|
parameter expansion then results in
|
|
|
|
parameter, with the smallest portion of the
|
|
|
|
suffix matched by the pattern deleted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter%%word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Remove Largest Suffix Pattern. The word
|
|
|
|
is expanded to produce a pattern. The
|
|
|
|
parameter expansion then results in
|
|
|
|
parameter, with the largest portion of the
|
|
|
|
suffix matched by the pattern deleted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter#word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Remove Smallest Prefix Pattern. The word
|
|
|
|
is expanded to produce a pattern. The
|
|
|
|
parameter expansion then results in
|
|
|
|
parameter, with the smallest portion of the
|
|
|
|
prefix matched by the pattern deleted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It Li ${parameter##word}
|
1994-05-26 06:18:55 +00:00
|
|
|
Remove Largest Prefix Pattern. The word
|
|
|
|
is expanded to produce a pattern. The
|
|
|
|
parameter expansion then results in
|
|
|
|
parameter, with the largest portion of the
|
|
|
|
prefix matched by the pattern deleted.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Ss Command Substitution
|
1994-05-26 06:18:55 +00:00
|
|
|
Command substitution allows the output of a command to be substituted in
|
|
|
|
place of the command name itself. Command substitution occurs when
|
|
|
|
the command is enclosed as follows:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
$(command)
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
or the backquoted version:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
`command`
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The shell expands the command substitution by executing command in a
|
|
|
|
subshell environment and replacing the command substitution
|
1999-10-07 13:56:43 +00:00
|
|
|
with the standard output of the command,
|
|
|
|
removing sequences of one or more newlines at the end of the substitution.
|
|
|
|
Embedded newlines before the end of the output are not removed;
|
|
|
|
however, during field splitting, they may be translated into spaces
|
|
|
|
depending on the value of
|
|
|
|
.Ev IFS
|
|
|
|
and the quoting that is in effect.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Arithmetic Expansion
|
1994-05-26 06:18:55 +00:00
|
|
|
Arithmetic expansion provides a mechanism for evaluating an arithmetic
|
1999-09-14 11:46:04 +00:00
|
|
|
expression and substituting its value.
|
|
|
|
The format for arithmetic expansion is as follows:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
$((expression))
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
The expression is treated as if it were in double-quotes, except
|
|
|
|
that a double-quote inside the expression is not treated specially. The
|
|
|
|
shell expands all tokens in the expression for parameter expansion,
|
|
|
|
command substitution, and quote removal.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
Next, the shell treats this as an arithmetic expression and
|
|
|
|
substitutes the value of the expression.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss White Space Splitting (Field Splitting)
|
1994-05-26 06:18:55 +00:00
|
|
|
After parameter expansion, command substitution, and
|
|
|
|
arithmetic expansion the shell scans the results of
|
|
|
|
expansions and substitutions that did not occur in double-quotes for
|
|
|
|
field splitting and multiple fields can result.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
The shell treats each character of the
|
|
|
|
.Ev IFS
|
|
|
|
as a delimiter and uses
|
1994-05-26 06:18:55 +00:00
|
|
|
the delimiters to split the results of parameter expansion and command
|
|
|
|
substitution into fields.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ss Pathname Expansion (File Name Generation)
|
1999-10-07 13:56:43 +00:00
|
|
|
Unless the
|
|
|
|
.Fl f
|
|
|
|
option is set,
|
|
|
|
file name generation is performed
|
1996-12-14 06:20:03 +00:00
|
|
|
after word splitting is complete. Each word is
|
1994-05-26 06:18:55 +00:00
|
|
|
viewed as a series of patterns, separated by slashes. The
|
|
|
|
process of expansion replaces the word with the names of
|
|
|
|
all existing files whose names can be formed by replacing
|
|
|
|
each pattern with a string that matches the specified pattern.
|
1996-12-14 06:20:03 +00:00
|
|
|
There are two restrictions on this: first, a pattern cannot match
|
|
|
|
a string containing a slash, and second,
|
1994-05-26 06:18:55 +00:00
|
|
|
a pattern cannot match a string starting with a period
|
|
|
|
unless the first character of the pattern is a period.
|
|
|
|
The next section describes the patterns used for both
|
1997-04-28 03:20:09 +00:00
|
|
|
Pathname Expansion and the
|
1999-09-08 15:40:46 +00:00
|
|
|
.Ic case
|
1997-04-28 03:20:09 +00:00
|
|
|
command.
|
|
|
|
.Ss Shell Patterns
|
1995-05-05 16:49:15 +00:00
|
|
|
A pattern consists of normal characters, which match themselves,
|
1999-10-07 13:56:43 +00:00
|
|
|
and meta-characters.
|
|
|
|
The meta-characters are
|
2000-12-18 14:48:17 +00:00
|
|
|
.Dq Li \&! ,
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li * ,
|
2000-12-18 14:48:17 +00:00
|
|
|
.Dq Li \&? ,
|
1999-08-30 10:13:04 +00:00
|
|
|
and
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li [ .
|
1999-08-30 10:13:04 +00:00
|
|
|
These characters lose their special meanings if they are quoted.
|
|
|
|
When command or variable substitution is performed and the dollar sign
|
|
|
|
or back quotes are not double-quoted, the value of the
|
1994-05-26 06:18:55 +00:00
|
|
|
variable or the output of the command is scanned for these
|
|
|
|
characters and they are turned into meta-characters.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-08-30 10:13:04 +00:00
|
|
|
An asterisk
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pq Li *
|
1999-11-18 08:13:21 +00:00
|
|
|
matches any string of characters.
|
|
|
|
A question mark
|
2000-12-18 14:48:17 +00:00
|
|
|
.Pq Li \&?
|
1999-11-18 08:13:21 +00:00
|
|
|
matches any single character.
|
1999-09-14 11:46:04 +00:00
|
|
|
A left bracket
|
1999-10-07 13:56:43 +00:00
|
|
|
.Pq Li [
|
1999-09-14 11:46:04 +00:00
|
|
|
introduces a character class.
|
|
|
|
The end of the character class is indicated by a
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li \&] ;
|
1999-08-30 10:13:04 +00:00
|
|
|
if the
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li \&]
|
1999-08-30 10:13:04 +00:00
|
|
|
is missing then the
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li [
|
1999-08-30 10:13:04 +00:00
|
|
|
matches a
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq Li [
|
1999-09-14 11:46:04 +00:00
|
|
|
rather than introducing a character class.
|
|
|
|
A character class matches any of the characters between the square brackets.
|
|
|
|
A range of characters may be specified using a minus sign.
|
1999-11-18 08:13:21 +00:00
|
|
|
The character class may be complemented by making an exclamation point
|
2000-12-18 14:48:17 +00:00
|
|
|
.Pq Li !\&
|
1999-11-18 08:13:21 +00:00
|
|
|
the first character of the character class.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-08-30 10:13:04 +00:00
|
|
|
To include a
|
2000-12-15 17:37:31 +00:00
|
|
|
.Dq Li \&]
|
1999-09-14 11:46:04 +00:00
|
|
|
in a character class, make it the first character listed
|
|
|
|
(after the
|
2000-12-18 14:48:17 +00:00
|
|
|
.Dq Li \&! ,
|
1999-09-14 11:46:04 +00:00
|
|
|
if any).
|
1999-11-18 08:13:21 +00:00
|
|
|
To include a
|
|
|
|
.Dq Li - ,
|
|
|
|
make it the first or last character listed.
|
1999-09-08 15:40:46 +00:00
|
|
|
.Ss Builtin Commands
|
|
|
|
This section lists the commands which
|
1997-04-28 03:20:09 +00:00
|
|
|
are builtin because they need to perform some operation
|
1999-09-08 15:40:46 +00:00
|
|
|
that can not be performed by a separate process. In addition to
|
2000-07-17 23:37:55 +00:00
|
|
|
these, a builtin version of the
|
1999-09-08 15:40:46 +00:00
|
|
|
.Xr printf 1
|
2000-07-17 23:37:55 +00:00
|
|
|
command is provided for efficiency.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
2001-02-01 16:38:02 +00:00
|
|
|
.It Ic \&:
|
1996-09-01 10:22:36 +00:00
|
|
|
A null command that returns a 0 (true) exit value.
|
2001-02-01 16:38:02 +00:00
|
|
|
.It Ic \&. Ar file
|
1996-09-01 10:22:36 +00:00
|
|
|
The commands in the specified file are read and executed by the shell.
|
1998-02-12 01:23:11 +00:00
|
|
|
If
|
|
|
|
.Ar file
|
|
|
|
contains any
|
1999-10-07 13:56:43 +00:00
|
|
|
.Dq /
|
1998-02-12 01:23:11 +00:00
|
|
|
characters, it is used as is. Otherwise, the shell searches the
|
|
|
|
.Ev PATH
|
|
|
|
for the file. If it is not found in the
|
|
|
|
.Ev PATH ,
|
|
|
|
it is sought in the current working directory.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic alias Op Ar name ...
|
2001-02-01 16:38:02 +00:00
|
|
|
.It Ic alias Xo
|
|
|
|
.Op Ar name Ns = Ns Ar string ...
|
|
|
|
.Xc
|
1999-10-07 13:56:43 +00:00
|
|
|
If
|
2001-02-01 16:38:02 +00:00
|
|
|
.Ar name Ns = Ns Ar string
|
1999-10-07 13:56:43 +00:00
|
|
|
is specified, the shell defines the alias
|
|
|
|
.Ar name
|
1999-08-30 10:13:04 +00:00
|
|
|
with value
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ar string .
|
1999-08-30 10:13:04 +00:00
|
|
|
If just
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ar name
|
1999-08-30 10:13:04 +00:00
|
|
|
is specified, the value of the alias
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ar name
|
1999-08-30 10:13:04 +00:00
|
|
|
is printed.
|
1999-10-07 13:56:43 +00:00
|
|
|
With no arguments, the
|
|
|
|
.Ic alias
|
|
|
|
builtin command prints the names and values of all defined aliases
|
|
|
|
(see
|
|
|
|
.Ic unalias ) .
|
|
|
|
.It Ic bg Op Ar job ...
|
|
|
|
Continue the specified jobs
|
|
|
|
(or the current job if no jobs are given)
|
|
|
|
in the background.
|
|
|
|
.It Ic command Ar cmd Op Ar arg ...
|
|
|
|
Execute the specified builtin command,
|
|
|
|
.Ar cmd .
|
|
|
|
This is useful when the user wishes to override a shell function
|
|
|
|
with the same name as a builtin command.
|
|
|
|
.It Ic cd Op Ar directory
|
|
|
|
Switch to the specified
|
|
|
|
.Ar directory ,
|
|
|
|
or to the directory specified in the
|
|
|
|
.Ev HOME
|
|
|
|
environment variable if no
|
|
|
|
.Ar directory
|
|
|
|
is specified.
|
|
|
|
If
|
|
|
|
.Ar directory
|
|
|
|
is not found as a subdirectory of the current directory
|
|
|
|
(and does not begin with
|
|
|
|
.Dq Li / ,
|
|
|
|
.Dq Li ./ ,
|
|
|
|
or
|
|
|
|
.Dq Li ../ ) ,
|
|
|
|
then the directories listed in the
|
|
|
|
.Ev CDPATH
|
|
|
|
variable will be
|
|
|
|
searched for the specified
|
|
|
|
.Ar directory .
|
|
|
|
The format of
|
|
|
|
.Ar CDPATH
|
|
|
|
is the same as that of
|
|
|
|
.Ev PATH .
|
1999-09-14 11:46:04 +00:00
|
|
|
In an interactive shell,
|
1999-10-07 13:56:43 +00:00
|
|
|
the
|
|
|
|
.Ic cd
|
|
|
|
command will print out the name of the directory
|
1999-09-14 11:46:04 +00:00
|
|
|
that it actually switched to
|
|
|
|
if this is different from the name that the user gave.
|
1999-10-07 13:56:43 +00:00
|
|
|
These may be different either because the
|
|
|
|
.Ev CDPATH
|
|
|
|
mechanism was used or because a symbolic link was crossed.
|
2001-04-28 02:13:58 +00:00
|
|
|
.It Ic chdir
|
|
|
|
A synonym for the
|
|
|
|
.Ic cd
|
|
|
|
builtin command.
|
2000-07-17 23:37:55 +00:00
|
|
|
.It Xo
|
|
|
|
.Ic echo
|
|
|
|
.Op Fl en
|
|
|
|
.Ar string
|
|
|
|
.Xc
|
|
|
|
Print
|
|
|
|
.Ar string
|
|
|
|
to the standard output with a newline appended.
|
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It Fl n
|
|
|
|
Suppress the output of the trailing newline.
|
|
|
|
.It Fl e
|
|
|
|
Process C-style backslash escape sequences.
|
|
|
|
.Ic echo
|
|
|
|
understands the following character escapes:
|
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It \ea
|
|
|
|
Alert (ring the terminal bell)
|
|
|
|
.It \eb
|
|
|
|
Backspace
|
|
|
|
.It \ec
|
|
|
|
Suppress the trailing newline (this has the side-effect of truncating the
|
|
|
|
line if it is not the last character)
|
|
|
|
.It \ee
|
|
|
|
The ESC character (ASCII 0x1b)
|
|
|
|
.It \ef
|
|
|
|
Formfeed
|
|
|
|
.It \en
|
|
|
|
Newline
|
|
|
|
.It \er
|
|
|
|
Carriage return
|
|
|
|
.It \et
|
|
|
|
Horizontal tab
|
|
|
|
.It \ev
|
|
|
|
Vertical tab
|
|
|
|
.It \e\e
|
|
|
|
Literal backslash
|
|
|
|
.It \e0nnn
|
|
|
|
(Zero) The character whose octal value is nnn
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Ar string
|
|
|
|
is not enclosed in quotes then the backslash itself must be escaped
|
|
|
|
with a backslash to protect it from the shell. For example
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
$ echo -e "a\evb"
|
|
|
|
a
|
|
|
|
b
|
|
|
|
$ echo -e a\e\evb
|
|
|
|
a
|
|
|
|
b
|
|
|
|
$ echo -e "a\e\eb"
|
|
|
|
a\eb
|
|
|
|
$ echo -e a\e\e\e\eb
|
|
|
|
a\eb
|
|
|
|
.Ed
|
|
|
|
.El
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic eval Ar string ...
|
1999-09-14 11:46:04 +00:00
|
|
|
Concatenate all the arguments with spaces.
|
|
|
|
Then re-parse and execute the command.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic exec Op Ar command Op arg ...
|
|
|
|
Unless
|
|
|
|
.Ar command
|
|
|
|
is omitted,
|
|
|
|
the shell process is replaced with the specified program
|
|
|
|
(which must be a real program, not a shell builtin command or function).
|
|
|
|
Any redirections on the
|
|
|
|
.Ic exec
|
|
|
|
command are marked as permanent,
|
|
|
|
so that they are not undone when the
|
|
|
|
.Ic exec
|
|
|
|
command finishes.
|
|
|
|
.It Ic exit Op Ar exitstatus
|
|
|
|
Terminate the shell process.
|
|
|
|
If
|
|
|
|
.Ar exitstatus
|
|
|
|
is given
|
|
|
|
it is used as the exit status of the shell;
|
|
|
|
otherwise the exit status of the preceding command is used.
|
|
|
|
.It Ic export Ar name ...
|
1994-05-26 06:18:55 +00:00
|
|
|
The specified names are exported so that they will
|
|
|
|
appear in the environment of subsequent commands.
|
1999-10-07 13:56:43 +00:00
|
|
|
The only way to un-export a variable is to
|
|
|
|
.Ic unset
|
|
|
|
it.
|
|
|
|
The shell allows the value of a variable to be set
|
|
|
|
at the same time as it is exported by writing
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
export name=value
|
|
|
|
.Ed
|
|
|
|
.Pp
|
1994-05-26 06:18:55 +00:00
|
|
|
With no arguments the export command lists the names
|
|
|
|
of all exported variables.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Xo
|
|
|
|
.Ic fc
|
|
|
|
.Op Fl e Ar editor
|
|
|
|
.Op Ar first Op Ar last
|
|
|
|
.Xc
|
|
|
|
.It Xo
|
|
|
|
.Ic fc
|
|
|
|
.Fl l
|
|
|
|
.Op Fl nr
|
|
|
|
.Op Ar first Op Ar last
|
|
|
|
.Xc
|
|
|
|
.It Xo
|
|
|
|
.Ic fc
|
|
|
|
.Fl s
|
|
|
|
.Op Ar old=new
|
|
|
|
.Op Ar first
|
|
|
|
.Xc
|
|
|
|
The
|
|
|
|
.Ic fc
|
|
|
|
builtin command lists, or edits and re-executes,
|
|
|
|
commands previously entered to an interactive shell.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl e Ar editor
|
|
|
|
Use the editor named by
|
|
|
|
.Ar editor
|
|
|
|
to edit the commands.
|
|
|
|
The editor string is a command name,
|
|
|
|
subject to search via the
|
|
|
|
.Ev PATH
|
|
|
|
variable.
|
|
|
|
The value in the
|
|
|
|
.Ev FCEDIT
|
|
|
|
variable is used as a default when
|
|
|
|
.Fl e
|
|
|
|
is not specified.
|
|
|
|
If
|
|
|
|
.Ev FCEDIT
|
|
|
|
is null or unset, the value of the
|
|
|
|
.Ev EDITOR
|
|
|
|
variable is used.
|
|
|
|
If
|
|
|
|
.Ev EDITOR
|
|
|
|
is null or unset,
|
1997-04-28 03:20:09 +00:00
|
|
|
.Xr ed 1
|
|
|
|
is used as the editor.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl l No (ell)
|
1994-05-26 06:18:55 +00:00
|
|
|
List the commands rather than invoking
|
|
|
|
an editor on them. The commands are written in the
|
|
|
|
sequence indicated by the first and last operands, as
|
1999-10-07 13:56:43 +00:00
|
|
|
affected by
|
|
|
|
.Fl r ,
|
|
|
|
with each command preceded by the command number.
|
|
|
|
.It Fl n
|
|
|
|
Suppress command numbers when listing with
|
|
|
|
.Fl l .
|
|
|
|
.It Fl r
|
|
|
|
Reverse the order of the commands listed
|
|
|
|
(with
|
|
|
|
.Fl l )
|
|
|
|
or edited
|
|
|
|
(with neither
|
|
|
|
.Fl l
|
|
|
|
nor
|
|
|
|
.Fl s ) .
|
|
|
|
.It Fl s
|
1994-05-26 06:18:55 +00:00
|
|
|
Re-execute the command without invoking an editor.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ar first
|
|
|
|
.It Ar last
|
|
|
|
Select the commands to list or edit.
|
|
|
|
The number of previous commands that can be accessed
|
|
|
|
are determined by the value of the
|
2001-02-01 16:38:02 +00:00
|
|
|
.Ev HISTSIZE
|
|
|
|
variable.
|
1999-10-07 13:56:43 +00:00
|
|
|
The value of
|
|
|
|
.Ar first
|
|
|
|
or
|
|
|
|
.Ar last
|
|
|
|
or both are one of the following:
|
|
|
|
.Bl -tag -width Ds
|
|
|
|
.It Ar [+]num
|
|
|
|
A positive number representing a command number;
|
|
|
|
command numbers can be displayed with the
|
|
|
|
.Fl l
|
|
|
|
option.
|
|
|
|
.It Ar -num
|
1994-05-26 06:18:55 +00:00
|
|
|
A negative decimal number representing the
|
1999-10-07 13:56:43 +00:00
|
|
|
command that was executed
|
|
|
|
.Ar num
|
|
|
|
of
|
|
|
|
commands previously.
|
|
|
|
For example, -1 is the immediately previous command.
|
|
|
|
.It Ar string
|
|
|
|
A string indicating the most recently entered command
|
|
|
|
that begins with that string.
|
|
|
|
If the
|
|
|
|
.Ar old=new
|
|
|
|
operand is not also specified with
|
|
|
|
.Fl s ,
|
|
|
|
the string form of the first operand cannot contain an embedded equal sign.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
1999-10-07 13:56:43 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The following environment variables affect the execution of
|
|
|
|
.Ic fc :
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ev FCEDIT
|
1994-05-26 06:18:55 +00:00
|
|
|
Name of the editor to use.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ev HISTSIZE
|
1998-05-18 06:44:24 +00:00
|
|
|
The number of previous commands that are accessible.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic fg Op Ar job
|
|
|
|
Move the specified
|
|
|
|
.Ar job
|
|
|
|
or the current job to the foreground.
|
|
|
|
.It Ic getopts Ar optstring Ar var
|
|
|
|
The POSIX
|
|
|
|
.Ic getopts
|
|
|
|
command.
|
|
|
|
The
|
|
|
|
.Ic getopts
|
|
|
|
command deprecates the older
|
|
|
|
.Xr getopt 1
|
|
|
|
command.
|
1996-12-14 06:20:03 +00:00
|
|
|
The first argument should be a series of letters, each possibly
|
|
|
|
followed by a colon which indicates that the option takes an argument.
|
|
|
|
The specified variable is set to the parsed option. The index of
|
1999-10-07 13:56:43 +00:00
|
|
|
the next argument is placed into the shell variable
|
|
|
|
.Ev OPTIND .
|
1996-12-14 06:20:03 +00:00
|
|
|
If an option takes an argument, it is placed into the shell variable
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ev OPTARG .
|
|
|
|
If an invalid option is encountered,
|
|
|
|
.Ev var
|
|
|
|
is set to
|
2000-12-18 14:48:17 +00:00
|
|
|
.Dq Li \&? .
|
1996-12-14 06:20:03 +00:00
|
|
|
It returns a false value (1) when it encounters the end of the options.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Xo
|
|
|
|
.Ic hash
|
|
|
|
.Op Fl rv
|
|
|
|
.Op Ar command ...
|
|
|
|
.Xc
|
|
|
|
The shell maintains a hash table which remembers the locations of commands.
|
|
|
|
With no arguments whatsoever, the
|
|
|
|
.Ic hash
|
|
|
|
command prints out the contents of this table.
|
|
|
|
Entries which have not been looked at since the last
|
|
|
|
.Ic cd
|
|
|
|
command are marked with an asterisk;
|
|
|
|
it is possible for these entries to be invalid.
|
|
|
|
.Pp
|
|
|
|
With arguments, the
|
|
|
|
.Ic hash
|
|
|
|
command removes each specified
|
|
|
|
.Ar command
|
|
|
|
from the hash table (unless they are functions) and then locates it.
|
|
|
|
With the
|
|
|
|
.Fl v
|
|
|
|
option,
|
|
|
|
.Ic hash
|
|
|
|
prints the locations of the commands as it finds them.
|
|
|
|
The
|
|
|
|
.Fl r
|
|
|
|
option causes the
|
|
|
|
.Ic hash
|
|
|
|
command to delete all the entries in the hash table except for functions.
|
|
|
|
.It Ic jobid Op Ar job
|
|
|
|
Print the process id's of the processes in the specified
|
|
|
|
.Ar job .
|
|
|
|
If the
|
|
|
|
.Ar job
|
|
|
|
argument is omitted, use the current job.
|
|
|
|
.It Ic jobs
|
1994-05-26 06:18:55 +00:00
|
|
|
This command lists out all the background processes
|
|
|
|
which are children of the current shell process.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic pwd
|
|
|
|
Print the path of the current directory. The builtin command may
|
1994-05-26 06:18:55 +00:00
|
|
|
differ from the program of the same name because the
|
|
|
|
builtin command remembers what the current directory
|
|
|
|
is rather than recomputing it each time. This makes
|
|
|
|
it faster. However, if the current directory is
|
1999-10-07 13:56:43 +00:00
|
|
|
renamed,
|
|
|
|
the builtin version of
|
|
|
|
.Xr pwd 1
|
|
|
|
will continue to print the old name for the directory.
|
|
|
|
.It Xo
|
|
|
|
.Ic read
|
|
|
|
.Op Fl p Ar prompt
|
|
|
|
.Op Fl t Ar timeout
|
|
|
|
.Op Fl er
|
|
|
|
.Ar variable ...
|
|
|
|
.Xc
|
|
|
|
The
|
|
|
|
.Ar prompt
|
|
|
|
is printed if the
|
|
|
|
.Fl p
|
|
|
|
option is specified
|
1994-05-26 06:18:55 +00:00
|
|
|
and the standard input is a terminal. Then a line is
|
|
|
|
read from the standard input. The trailing newline
|
|
|
|
is deleted from the line and the line is split as
|
1999-10-07 13:56:43 +00:00
|
|
|
described in the section on
|
|
|
|
.Sx White Space Splitting (Field Splitting)
|
|
|
|
above, and
|
1994-05-26 06:18:55 +00:00
|
|
|
the pieces are assigned to the variables in order.
|
1995-05-05 16:49:15 +00:00
|
|
|
If there are more pieces than variables, the remaining
|
1999-10-07 13:56:43 +00:00
|
|
|
pieces (along with the characters in
|
|
|
|
.Ev IFS
|
|
|
|
that separated them)
|
|
|
|
are assigned to the last variable.
|
1995-05-05 16:49:15 +00:00
|
|
|
If there are more variables than pieces, the remaining
|
|
|
|
variables are assigned the null string.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
Backslashes are treated specially, unless the
|
|
|
|
.Fl r
|
|
|
|
option is
|
1999-08-26 08:16:27 +00:00
|
|
|
specified. If a backslash is followed by
|
|
|
|
a newline, the backslash and the newline will be
|
1999-10-07 13:56:43 +00:00
|
|
|
deleted. If a backslash is followed by any other
|
1999-08-26 08:16:27 +00:00
|
|
|
character, the backslash will be deleted and the following
|
1999-10-07 13:56:43 +00:00
|
|
|
character will be treated as though it were not in
|
|
|
|
.Ev IFS ,
|
|
|
|
even if it is.
|
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Fl t
|
|
|
|
option is specified and the
|
|
|
|
.Ar timeout
|
|
|
|
elapses before any input is supplied,
|
|
|
|
the
|
|
|
|
.Ic read
|
|
|
|
command will return without assigning any values.
|
|
|
|
The
|
|
|
|
.Ar timeout
|
|
|
|
value may optionally be followed by one of
|
|
|
|
.Dq s ,
|
|
|
|
.Dq m
|
|
|
|
or
|
|
|
|
.Dq h
|
|
|
|
to explicitly specify seconds, minutes or or hours.
|
|
|
|
If none is supplied,
|
|
|
|
.Dq s
|
|
|
|
is assumed.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fl e
|
|
|
|
option exists only for backward compatibility with older scripts.
|
|
|
|
.It Ic readonly Ar name ...
|
|
|
|
Each specified
|
|
|
|
.Ar name
|
|
|
|
is marked as read only,
|
|
|
|
so that it cannot be subsequently modified or unset.
|
|
|
|
The shell allows the value of a variable to be set
|
|
|
|
at the same time as it is marked read only
|
|
|
|
by using the following form:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bd -literal -offset indent
|
1994-05-26 06:18:55 +00:00
|
|
|
readonly name=value
|
1997-04-28 03:20:09 +00:00
|
|
|
.Ed
|
|
|
|
.Pp
|
1999-10-07 13:56:43 +00:00
|
|
|
With no arguments the
|
|
|
|
.Ic readonly
|
|
|
|
command lists the names of all read only variables.
|
|
|
|
.It Xo
|
|
|
|
.Ic set
|
|
|
|
.Op Fl /+abCEefIimnpTuVvx
|
|
|
|
.Op Fl /+o Ar longname
|
|
|
|
.Op Fl c Ar string
|
|
|
|
.Op Fl - Ar arg ...
|
|
|
|
.Xc
|
1999-11-18 08:13:21 +00:00
|
|
|
The
|
|
|
|
.Ic set
|
|
|
|
command performs three different functions:
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -item
|
|
|
|
.It
|
1999-10-07 13:56:43 +00:00
|
|
|
With no arguments, it lists the values of all shell variables.
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
1999-10-07 13:56:43 +00:00
|
|
|
If options are given,
|
|
|
|
either in short form or using the long
|
|
|
|
.Dq Fl /+o Ar longname
|
|
|
|
form,
|
|
|
|
it sets or clears the specified options as described in the section called
|
1999-08-30 10:13:04 +00:00
|
|
|
.Sx Argument List Processing .
|
1997-04-28 03:20:09 +00:00
|
|
|
.It
|
1999-10-07 13:56:43 +00:00
|
|
|
If the
|
|
|
|
.Dq Fl -
|
|
|
|
option is specified,
|
|
|
|
.Ic set
|
|
|
|
will replace the shell's positional parameters with the subsequent
|
|
|
|
arguments.
|
|
|
|
If no arguments follow the
|
|
|
|
.Dq Fl -
|
|
|
|
option,
|
|
|
|
all the positional parameters will be cleared,
|
|
|
|
which is equivalent to executing the command
|
|
|
|
.Dq Li shift $# .
|
|
|
|
The
|
|
|
|
.Dq Fl -
|
|
|
|
flag may be ommitted when specifying arguments to be used
|
|
|
|
as positional replacement parameters.
|
|
|
|
This is not recommended,
|
|
|
|
because the first argument may begin with a dash
|
|
|
|
.Pq Li -
|
|
|
|
or a plus
|
|
|
|
.Pq Li + ,
|
|
|
|
which the
|
|
|
|
.Ic set
|
|
|
|
command will interpret as a request to enable or disable options.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic setvar Ar variable Ar value
|
|
|
|
Assigns the specified
|
|
|
|
.Ar value
|
|
|
|
to the specified
|
|
|
|
.Ar variable .
|
|
|
|
.Ic Setvar
|
|
|
|
is intended to be used in functions that
|
1999-09-14 11:46:04 +00:00
|
|
|
assign values to variables whose names are passed as parameters.
|
1999-10-07 13:56:43 +00:00
|
|
|
In general it is better to write
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
variable=value
|
|
|
|
.Ed
|
|
|
|
rather than using
|
|
|
|
.Ic setvar .
|
|
|
|
.It Ic shift Op Ar n
|
|
|
|
Shift the positional parameters
|
|
|
|
.Ar n
|
1999-11-18 08:13:21 +00:00
|
|
|
times, or once if
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ar n
|
|
|
|
is not specified.
|
|
|
|
A shift sets the value of $1 to the value of $2,
|
|
|
|
the value of $2 to the value of $3, and so on,
|
|
|
|
decreasing the value of $# by one.
|
|
|
|
If there are zero positional parameters, shifting does not do anything.
|
|
|
|
.It Xo
|
|
|
|
.Ic trap
|
|
|
|
.Op Ar action
|
|
|
|
.Ar signal ...
|
|
|
|
.Xc
|
|
|
|
Cause the shell to parse and execute
|
|
|
|
.Ar action
|
|
|
|
when any specified
|
|
|
|
.Ar signal
|
|
|
|
is received.
|
|
|
|
The signals are specified by signal number.
|
|
|
|
The
|
|
|
|
.Ar action
|
|
|
|
may be null or omitted;
|
|
|
|
the former causes the specified signal to be ignored
|
|
|
|
and the latter causes the default action to be taken.
|
|
|
|
When the shell forks off a subshell,
|
|
|
|
it resets trapped (but not ignored) signals to the default action.
|
|
|
|
The
|
|
|
|
.Ic trap
|
|
|
|
command has no effect on signals that were ignored on entry to the shell.
|
|
|
|
.It Ic type Op Ar name ...
|
|
|
|
Interpret each
|
|
|
|
.Ar name
|
|
|
|
as a command and print the resolution of the command search.
|
1999-09-14 11:46:04 +00:00
|
|
|
Possible resolutions are:
|
1999-09-08 15:40:46 +00:00
|
|
|
shell keyword, alias, shell builtin command, command, tracked alias
|
1999-10-07 13:56:43 +00:00
|
|
|
and not found.
|
|
|
|
For aliases the alias expansion is printed;
|
|
|
|
for commands and tracked aliases
|
|
|
|
the complete pathname of the command is printed.
|
|
|
|
.It Xo
|
|
|
|
.Ic ulimit
|
1999-12-15 16:12:32 +00:00
|
|
|
.Op Fl HSabcdflmnust
|
1999-10-07 13:56:43 +00:00
|
|
|
.Op Ar limit
|
|
|
|
.Xc
|
1997-04-28 03:20:09 +00:00
|
|
|
Set or display resource limits (see
|
2001-01-16 09:08:22 +00:00
|
|
|
.Xr getrlimit 2 ) .
|
1999-08-30 10:13:04 +00:00
|
|
|
If
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ar limit
|
1999-08-30 10:13:04 +00:00
|
|
|
is specified, the named resource will be set;
|
1995-10-19 18:42:12 +00:00
|
|
|
otherwise the current resource value will be displayed.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-08-30 10:13:04 +00:00
|
|
|
If
|
1999-10-07 13:56:43 +00:00
|
|
|
.Fl H
|
|
|
|
is specified, the hard limits will be set or displayed.
|
|
|
|
While everybody is allowed to reduce a hard limit,
|
|
|
|
only the superuser can increase it.
|
|
|
|
The
|
|
|
|
.Fl S
|
|
|
|
option
|
1995-10-19 18:42:12 +00:00
|
|
|
specifies the soft limits instead. When displaying limits,
|
1999-08-30 10:13:04 +00:00
|
|
|
only one of
|
1999-10-07 13:56:43 +00:00
|
|
|
.Fl S
|
1999-08-30 10:13:04 +00:00
|
|
|
or
|
1999-10-07 13:56:43 +00:00
|
|
|
.Fl H
|
|
|
|
can be given.
|
|
|
|
The default is to display the soft limits,
|
|
|
|
and to set both the hard and the soft limits.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1999-08-30 10:13:04 +00:00
|
|
|
Option
|
1999-10-07 13:56:43 +00:00
|
|
|
.Fl a
|
|
|
|
causes the
|
|
|
|
.Ic ulimit
|
|
|
|
command to display all resources.
|
|
|
|
The parameter
|
|
|
|
.Ar limit
|
1999-08-30 10:13:04 +00:00
|
|
|
is not acceptable in this mode.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Pp
|
1995-10-19 18:42:12 +00:00
|
|
|
The remaining options specify which resource value is to be
|
1999-10-07 13:56:43 +00:00
|
|
|
displayed or modified.
|
|
|
|
They are mutually exclusive.
|
1997-04-28 03:20:09 +00:00
|
|
|
.Bl -tag -width Ds
|
1999-12-15 16:12:32 +00:00
|
|
|
.It Fl b Ar sbsize
|
|
|
|
The maximum size of socket buffer usage, in bytes.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl c Ar coredumpsize
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal size of core dump files, in 512-byte blocks.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl d Ar datasize
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal size of the data segment of a process, in kilobytes.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl f Ar filesize
|
|
|
|
The maximal size of a file, in 512-byte blocks.
|
|
|
|
.It Fl l Ar lockedmem
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal size of memory that can be locked by a process, in
|
|
|
|
kilobytes.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl m Ar memoryuse
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal resident set size of a process, in kilobytes.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl n Ar nofiles
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal number of descriptors that could be opened by a process.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl s Ar stacksize
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal size of the stack segment, in kilobytes.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl t Ar time
|
1995-10-19 18:42:12 +00:00
|
|
|
The maximal amount of CPU time to be used by each process, in seconds.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Fl u Ar userproc
|
1997-04-28 03:20:09 +00:00
|
|
|
The maximal number of simultaneous processes for this user ID.
|
|
|
|
.El
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic umask Op Ar mask
|
|
|
|
Set the file creation mask (see
|
1997-04-28 03:20:09 +00:00
|
|
|
.Xr umask 2 )
|
1999-10-07 13:56:43 +00:00
|
|
|
to the octal value specified by
|
2000-07-24 14:59:50 +00:00
|
|
|
.Ar mask .
|
1999-10-07 13:56:43 +00:00
|
|
|
If the argument is omitted, the current mask value is printed.
|
|
|
|
.It Xo
|
|
|
|
.Ic unalias
|
|
|
|
.Op Fl a
|
|
|
|
.Op Ar name
|
|
|
|
.Xc
|
1999-08-30 10:13:04 +00:00
|
|
|
If
|
1999-10-07 13:56:43 +00:00
|
|
|
.Ar name
|
1999-08-30 10:13:04 +00:00
|
|
|
is specified, the shell removes that alias.
|
|
|
|
If
|
2000-07-24 15:02:04 +00:00
|
|
|
.Fl a
|
1999-08-30 10:13:04 +00:00
|
|
|
is specified, all aliases are removed.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic unset Ar name ...
|
|
|
|
The specified variables and functions are unset and unexported.
|
|
|
|
If a given
|
|
|
|
.Ar name
|
|
|
|
corresponds to both a variable and a function,
|
1999-09-14 11:46:04 +00:00
|
|
|
both the variable and the function are unset.
|
1999-10-07 13:56:43 +00:00
|
|
|
.It Ic wait Op Ar job
|
|
|
|
Wait for the specified
|
|
|
|
.Ar job
|
|
|
|
to complete and return the exit status of the last process in the
|
|
|
|
.Ar job .
|
1999-09-14 11:46:04 +00:00
|
|
|
If the argument is omitted, wait for all jobs to complete
|
|
|
|
and return an exit status of zero.
|
1997-04-28 03:20:09 +00:00
|
|
|
.El
|
|
|
|
.Ss Commandline Editing
|
|
|
|
When
|
|
|
|
.Nm
|
|
|
|
is being used interactively from a terminal, the current command
|
1999-10-07 13:56:43 +00:00
|
|
|
and the command history
|
|
|
|
(see
|
|
|
|
.Ic fc
|
|
|
|
in
|
1999-09-08 15:40:46 +00:00
|
|
|
.Sx Builtin Commands )
|
1999-10-07 13:56:43 +00:00
|
|
|
can be edited using vi-mode command line editing.
|
|
|
|
This mode uses commands similar
|
1994-05-26 06:18:55 +00:00
|
|
|
to a subset of those described in the vi man page.
|
1999-10-07 13:56:43 +00:00
|
|
|
The command
|
|
|
|
.Dq Li set -o vi
|
|
|
|
(or
|
1999-11-18 08:13:21 +00:00
|
|
|
.Dq Li set -V )
|
1999-10-07 13:56:43 +00:00
|
|
|
enables vi-mode editing and places
|
1997-04-28 03:20:09 +00:00
|
|
|
.Nm
|
|
|
|
into vi insert mode. With vi-mode enabled,
|
|
|
|
.Nm
|
1999-10-07 13:56:43 +00:00
|
|
|
can be switched between insert mode and command mode by typing
|
|
|
|
.Aq ESC .
|
|
|
|
Hitting
|
|
|
|
.Aq return
|
|
|
|
while in command mode will pass the line to the shell.
|
|
|
|
.Pp
|
|
|
|
Similarly, the
|
|
|
|
.Dq Li set -o emacs
|
|
|
|
(or
|
1999-11-18 08:13:21 +00:00
|
|
|
.Dq Li set -E )
|
1999-10-07 13:56:43 +00:00
|
|
|
command can be used to enable a subset of
|
1999-08-30 10:13:04 +00:00
|
|
|
emacs-style command line editing features.
|
1998-12-18 03:51:18 +00:00
|
|
|
.Sh SEE ALSO
|
1999-09-08 15:40:46 +00:00
|
|
|
.Xr builtin 1 ,
|
|
|
|
.Xr echo 1 ,
|
1998-12-18 03:51:18 +00:00
|
|
|
.Xr expr 1 ,
|
1999-09-08 15:40:46 +00:00
|
|
|
.Xr pwd 1 ,
|
|
|
|
.Xr printf 1 ,
|
1998-12-18 03:51:18 +00:00
|
|
|
.Xr test 1
|
1997-04-28 03:20:09 +00:00
|
|
|
.Sh HISTORY
|
1996-08-29 18:06:19 +00:00
|
|
|
A
|
1997-04-28 03:20:09 +00:00
|
|
|
.Nm
|
1996-08-29 18:06:19 +00:00
|
|
|
command appeared in
|
1997-04-28 03:20:09 +00:00
|
|
|
.At V.1 .
|