📄 ash.1
字号:
SH(1) Minix Programmer's Manual SH(1)
NAME
ash, command, getopts, hash, jobs, local, return, setvar, unset - a shell
SYNOPSIS
ash [ -efIijnsxz ] [ +efIijnsxz ] [ -c command ] [ arg ] ...
COPYRIGHT
Copyright 1989 by Kenneth Almquist.
DESCRIPTION
Ash is a version of sh with features similar to those of the System V
shell. This manual page lists all the features of ash but concentrates
on the ones not in other shells.
Invocation
If the -c options is given, then the shell executes the specified shell
command. The -s flag cause the shell to read commands from the standard
input (after executing any command specified with the -c option. If
neither the -s or -c options are set, then the first arg is taken as the
name of a file to read commands from. If this is impossible because
there are no arguments following the options, then ash will set the -s
flag and will read commands from the standard input.
The shell sets the initial value of the positional parameters from the
args remaining after any arg used as the name of a file of commands is
deleted.
The flags (other than -c) are set by preceding them with ``-'' and
cleared by preceding them with ``+''; see the set builtin command for a
list of flags. If no value is specified for the -i flag, the -s flag is
set, and the standard input and output of the shell are connected to
terminals, then the -i flag will be set. If no value is specified for
the -j flag, then the -j flag will be set if the -i flag is set.
When the shell is invoked with the -c option, it is good practice to
include the -i flag if the command was entered interactively by a user.
For compatibility with the System V shell, the -i option should come
after the -c option.
If the first character of argument zero to the shell is ``-'', the shell
is assumed to be a login shell, and the files /etc/profile and .profile
are read if they exist. If the environment variable SHINIT is set on
entry to the shell, the commands in SHINIT are normally parsed and
executed. SHINIT is not examined if the shell is a login shell, or if it
the shell is running a shell procedure. (A shell is considered to be
running a shell procedure if neither the -s nor the -c options are set.)
Control Structures
7BSD March 7, 1991 1
SH(1) Minix Programmer's Manual SH(1)
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. (This differs from the System V shell, which requires
a list to contain at least one command in most cases.) 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.
``&&'' and ``||'' are binary operators. ``&&'' executes the first
command, and then executes the second command iff the exit status of the
first command is zero. ``||'' is similar, but executes the second
command iff the exit status of the first command is nonzero. ``&&'' and
``||'' both have the same priority.
The ``|'' operator is a binary operator which feeds the standard output
of the first command into the standard input of the second command. The
exit status of the ``|'' operator is the exit status of the second
command. ``|'' has a higher priority than ``||'' or ``&&''.
An if command looks like
if list
then list
[ elif list
then list ] ...
[ else list ]
fi
A while command looks like
while list
do list
done
The two lists are executed repeatedly while the exit status of the first
list is zero. The until command is similar, but has the word until in
place of while
repeats until the exit status of the first list is zero.
The for command looks like
for variable in word...
do list
done
The words are expanded, and then the list is executed repeatedly with the
variable set to each word in turn. do and done may be replaced with
``{'' and ``}''.
7BSD March 7, 1991 2
SH(1) Minix Programmer's Manual SH(1)
The break and continue commands look like
break [ num ]
continue [ num ]
Break terminates the num innermost for or while loops. Continue
continues with the next iteration of the num'th innermost loop. These
are implemented as builtin commands.
The case command looks like
case word in
pattern) list ;;
...
esac
The pattern can actually be one or more patterns (see Patterns below),
separated by ``|'' characters.
Commands may be grouped by writing either
(list)
or
{ list; }
The first of these executes the commands in a subshell.
A function definition looks like
name ( ) command
A function definition is an executable statement; when executed it
installs a function named name and returns an exit status of zero. The
command is normally a list enclosed between ``{'' and ``}''.
Variables may be declared to be local to a function by using a local
command. This should appear as the first staement of a function, and
looks like
local [ variable | - ] ...
Local is implemented as a builtin command.
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 one. Otherwise, the variable is initially
unset. Ash uses dynamic scoping, so that if you make the variable x
local to function f, which then calls function g, references to the
7BSD March 7, 1991 3
SH(1) Minix Programmer's Manual SH(1)
variable x made inside g will refer to the variable x declared inside f,
not to the global variable named x.
The only special parameter than can be made local is ``-''. Making ``-''
local any shell options that are changed via the set command inside the
function to be restored to their original values when the function
returns.
The return command looks like
return [ exitstatus ]
It terminates the currently executing function. Return is implemented as
a builtin command.
Simple Commands
A simple command is a sequence of words. The execution of a simple
command proceeds as follows. First, the leading words of the form
``name=value'' are stripped off and assigned to the environment of the
command. Second, the words are expanded. Third, the first remaining
word is taken as the command name that command is located. Fourth, any
redirections are performed. Fifth, the command is executed. We look at
these operations in reverse order.
The execution of the command varies with the type of command. There are
three types of commands: shell functions, builtin commands, and normal
programs.
When a shell function is executed, all of the shell positional parameters
(except $0, which remains unchanged) are set to the parameters to 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 to 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.
Shell builtins are executed internally to the shell, without spawning a
new process.
When a normal program is executed, the shell runs the program, passing
the parameters and the environment to the program. If the program is a
shell procedure, the shell will interpret the program in a subshell. The
shell will reinitialize itself in this case, so that the effect will be
as if a new shell had been invoked to handle the shell procedure, except
that the location of commands located in the parent shell will be
remembered by the child. If the program is a file beginning with ``#!'',
the remainder of the first line specifies an interpreter for the program.
The shell (or the operating system, under Berkeley UNIX) will run the
7BSD March 7, 1991 4
SH(1) Minix Programmer's Manual SH(1)
interpreter in this case. The arguments to the interpreter will consist
of any arguments given on the first line of the program, followed by the
name of the program, followed by the arguments passed to the program.
Redirection
Input/output redirections can be intermixed with the words in a simple
command and can be placed following any of the other commands. When
redirection occurs, the shell saves the old values of the file
descriptors and restores them when the command completes. The ``<'',
``>'', and ``>>'' redirections open a file for input, output, and
appending, respectively. The ``<&digit'' and ``>&digit'' makes the input
or output a duplicate of the file descriptor numbered by the digit. If a
minus sign is used in place of a digit, the standard input or standard
output are closed.
The ``<< word'' redirection takes input from a here document. As the
shell encounters ``<<'' redirections, it collects them. The next time it
encounters an unescaped newline, it reads the documents in turn. The
word following the ``<<'' specifies the contents of the line that
terminates the document. If none of the quoting methods ('', "", or \)
are used to enter the word, then the document is treated like a word
inside double quotes: ``$'' and backquote are expanded and backslash can
be used to escape these and to continue long lines. The word cannot
contain any variable or command substitutions, and its length (after
quoting) must be in the range of 1 to 79 characters. If ``<<-'' is used
in place of ``<<'', then leading tabs are deleted from the lines of the
document. (This is to allow you do indent shell procedures containing
here documents in a natural fashion.)
Any of the preceding redirection operators may be preceded by a single
digit specifying the file descriptor to be redirected. There cannot be
any white space between the digit and the redirection operator.
Path Search
When locating a command, the shell first looks to see if it has a shell
function by that name. Then, if PATH does not contain an entry for
"%builtin", it looks for a builtin command by that name. Finally, it
searches each entry in PATH in turn for the command.
The value of the PATH variable should be a series of entries separated by
colons. Each entry consists of a directory name, or a directory name
followed by a flag beginning with a percent sign. The current directory
should be indicated by an empty directory name.
If no percent sign is present, then the entry causes the shell to search
for the command in the specified directory. If the flag is ``%builtin''
then the list of shell builtin commands is searched. If the flag is
``%func'' then the directory is searched for a file which is read as
7BSD March 7, 1991 5
SH(1) Minix Programmer's Manual SH(1)
input to the shell. This file should define a function whose name is the
name of the command being searched for.
Command names containing a slash are simply executed without performing
any of the above searches.
The Environment
The environment of a command is a set of name/value pairs. When the
shell is invoked, it reads these names and values, sets the shell
variables with these names to the corresponding values, and marks the
variables as exported. The export command can be used to mark additional
variables as exported.
The environment of a command is constructed by constructing name/value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -