sh, jsh - standard and job control shell and command interpreter
/usr/bin/sh [-acefhiknprstuvx] [argument]...
/usr/xpg4/bin/sh [± abCefhikmnoprstuvx] [± o option]... [-c string] [arg]...
/usr/bin/jsh [-acefhiknprstuvx] [argument]...
The /usr/bin/sh utility is a command programming language that executes commands read from a terminal or a file.
The /usr/xpg4/bin/sh utility is a standards compliant shell. This utility provides all the functionality of ksh(1), except in cases discussed in ksh(1) where differences in behavior exist.
The jsh utility is an interface to the shell that provides all of the functionality of sh and enables job control (see Job Control section below).
Arguments to the shell are listed in the Invocation section below.
A blank is a tab or a space. A name is a sequence of ASCII letters, digits, or underscores, beginning with a letter or an underscore. A parameter is a name, a digit, or any of the characters *, @, #, ?, -, $, and !.
A simple-command is a sequence of non-blank words separated by blanks. The first word specifies the name of the command to be executed. Except as specified below, the remaining words are passed as arguments to the invoked command. The command name is passed as argument 0 (see exec(2)). The value of a simple-command is its exit status if it terminates normally, or (octal) 200+status if it terminates abnormally. See signal.h(3HEAD) for a list of status values.
A pipeline is a sequence of one or more commands separated by |. The standard output of each command but the last is connected by a pipe(2) to the standard input of the next command. Each command is run as a separate process. The shell waits for the last command to terminate. The exit status of a pipeline is the exit status of the last command in the pipeline.
A list is a sequence of one or more pipelines separated by ;, &, &&, or ||, and optionally terminated by ; or &. Of these four symbols, ; and & have equal precedence, which is lower than that of && and ||. The symbols && and || also have equal precedence. A semicolon (;) causes sequential execution of the preceding pipeline, that is, the shell waits for the pipeline to finish before executing any commands following the semicolon. An ampersand (&) causes asynchronous execution of the preceding pipeline, that is, the shell does not wait for that pipeline to finish. The symbol && (||) causes the list following it to be executed only if the preceding pipeline returns a zero (non-zero) exit status. An arbitrary number of newlines can appear in a list, instead of semicolons, to delimit commands.
A command is either a simple-command or one of the following. Unless otherwise stated, the value returned by a command is that of the last simple-command executed in the command.
for name [ in word ... ] do list done
case word in [ pattern [ | pattern ] ) list ;; ] ... esac
if list ; then list elif list ; then list ; ] ... [ else list ; ] fi
The list following if is executed and, if it returns a zero exit status, the list following the first then is executed. Otherwise, the list following elif is executed and, if its value is zero, the list following the next then is executed. Failing that, the else list is executed. If no else list or then list is executed, then the if command returns a zero exit status.
while list do list done
(list)
{ list;}
name () { list;}
The following words are only recognized as the first word of a command and when not quoted:
if then else elif fi case esac for while until do done { }
A word beginning with # causes that word and all the following characters up to a newline to be ignored.
The shell reads commands from the string between two grave accents (``) and the standard output from these commands can be used as all or part of a word. Trailing newlines from the standard output are removed.
No interpretation is done on the string before the string is read, except to remove backslashes (\) used to escape other characters. Backslashes can be used to escape a grave accent (`) or another backslash (\) and are removed before the command string is read. Escaping grave accents allows nested command substitution. If the command substitution lies within a pair of double quotes (" ...` ...` ... "), a backslash used to escape a double quote (\") is removed. Otherwise, it is left intact.
If a backslash is used to escape a newline character (\newline), both the backslash and the newline are removed (see the later section on Quoting). In addition, backslashes used to escape dollar signs (\$) are removed. Since no parameter substitution is done on the command string before it is read, inserting a backslash to escape a dollar sign has no effect. Backslashes that precede characters other than \, `, ", newline, and $ are left intact when the command string is read.
The character $ is used to introduce substitutable parameters. There are two types of parameters, positional and keyword. If parameter is a digit, it is a positional parameter. Positional parameters can be assigned values by set. Keyword parameters (also known as variables) can be assigned values by writing:
name=value [ name=value ] ...
Pattern-matching is not performed on value. There cannot be a function and a variable with the same name.
${parameter}
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}
In the above, word is not evaluated unless it is to be used as the substituted string, so that, in the following example, pwd is executed only if d is not set or is null:
echo ${d:-`pwd`}
If the colon (:) is omitted from the above expressions, the shell only checks whether parameter is set or not.
The following parameters are automatically set by the shell.
#
-
?
$
!
The following parameters are used by the shell. The parameters in this section are also referred to as environment variables.
HOME
PATH
CDPATH
MAILCHECK
MAILPATH
PS1
PS2
IFS
SHACCT
SHELL
See environ(5) for descriptions of the following environment variables that affect the execution of sh: LC_CTYPE and LC_MESSAGES.
The shell gives default values to PATH, PS1, PS2, MAILCHECK, and IFS. Default values for HOME and MAIL are set by login(1).
After parameter and command substitution, the results of substitution are scanned for internal field separator characters (those found in IFS) and split into distinct arguments where such characters are found. Explicit null arguments ("" or '') are retained. Implicit null arguments (those resulting from parameters that have no values) are removed.
A command's input and output can be redirected using a special notation interpreted by the shell. The following can appear anywhere in a simple-command or can precede or follow a command and are not passed on as arguments to the invoked command. Note: Parameter and command substitution occurs before word or digit is used.
<word
>word
>>word
<>word
<<[-]word
<&digit
<&-
If any of the above is preceded by a digit, the file descriptor which is associated with the file is that specified by the digit (instead of the default 0 or 1). For example:
... 2>&1
associates file descriptor 2 with the file currently associated with file descriptor 1.
The order in which redirections are specified is significant. The shell evaluates redirections left-to-right. For example:
... 1>xxx 2>&1
first associates file descriptor 1 with file xxx. It associates file descriptor 2 with the file associated with file descriptor 1 (that is, xxx). If the order of redirections were reversed, file descriptor 2 would be associated with the terminal (assuming file descriptor 1 had been) and file descriptor 1 would be associated with file xxx.
Using the terminology introduced on the first page, under Commands, if a command is composed of several simple commands, redirection is evaluated for the entire command before it is evaluated for each simple command. That is, the shell evaluates redirection for the entire list, then each pipeline within the list, then each command within each pipeline, then each list within each command.
If a command is followed by &, the default standard input for the command is the empty file, /dev/null. Otherwise, the environment for the execution of a command contains the file descriptors of the invoking shell as modified by input/output specifications.
Before a command is executed, each command word is scanned for the characters *, ?, and [. If one of these characters appears the word is regarded as a pattern. The word is replaced with alphabetically sorted file names that match the pattern. If no file name is found that matches the pattern, the word is left unchanged. The character . at the start of a file name or immediately following a /, as well as the character / itself, must be matched explicitly.
*
?
[...]
Notice that all quoted characters (see below) must be matched explicitly in a filename.
The following characters have a special meaning to the shell and cause termination of a word unless quoted:
; & ( ) | ^ < > newline space tab
A character can be quoted (that is, made to stand for itself) by preceding it with a backslash (\) or inserting it between a pair of quote marks ('' or ""). During processing, the shell can quote certain characters to prevent them from taking on a special meaning. Backslashes used to quote a single character are removed from the word before the command is executed. The pair \newline is removed from a word before command and parameter substitution.
All characters enclosed between a pair of single quote marks (''), except a single quote, are quoted by the shell. Backslash has no special meaning inside a pair of single quotes. A single quote can be quoted inside a pair of double quote marks (for example, "'"), but a single quote can not be quoted inside a pair of single quotes.
Inside a pair of double quote marks (""), parameter and command substitution occurs and the shell quotes the results to avoid blank interpretation and file name generation. If $* is within a pair of double quotes, the positional parameters are substituted and quoted, separated by quoted spaces ("$1 $2 ..."). However, if $@ is within a pair of double quotes, the positional parameters are substituted and quoted, separated by unquoted spaces ("$1""$2" ... ). \ quotes the characters \, `, , (comma), and $. The pair \newline is removed before parameter and command substitution. If a backslash precedes characters other than \, `, , (comma), $, and newline, then the backslash itself is quoted by the shell.
When used interactively, the shell prompts with the value of PS1 before reading a command. If at any time a newline is typed and further input is needed to complete a command, the secondary prompt (that is, the value of PS2) is issued.
The environment (see environ(5)) is a list of name-value pairs that is passed to an executed program in the same way as a normal argument list. The shell interacts with the environment in several ways. On invocation, the shell scans the environment and creates a parameter for each name found, giving it the corresponding value. If the user modifies the value of any of these parameters or creates new parameters, none of these affects the environment unless the export command is used to bind the shell's parameter to the environment (see also set -a). A parameter can be removed from the environment with the unset command. The environment seen by any executed command is thus composed of any unmodified name-value pairs originally inherited by the shell, minus any pairs removed by unset, plus any modifications or additions, all of which must be noted in export commands.
The environment for any simple-command can be augmented by prefixing it with one or more assignments to parameters. Thus:
TERM=450 command
and
(export TERM; TERM=450; command
are equivalent as far as the execution of command is concerned if command is not a Special Command. If command is a Special Command, then
TERM=450 command
modifies the TERM variable in the current shell.
If the -k flag is set, all keyword arguments are placed in the environment, even if they occur after the command name. The following example first prints a=b c and c:
echo a=b c a=b c set -k echo a=b c c
The INTERRUPT and QUIT signals for an invoked command are ignored if the command is followed by &. Otherwise, signals have the values inherited by the shell from its parent, with the exception of signal 11 (but see also the trap command below).
Each time a command is executed, the command substitution, parameter substitution, blank interpretation, input/output redirection, and filename generation listed above are carried out. If the command name matches the name of a defined function, the function is executed in the shell process (note how this differs from the execution of shell script files, which require a sub-shell for invocation). If the command name does not match the name of a defined function, but matches one of the Special Commands listed below, it is executed in the shell process.
The positional parameters $1, $2, ... are set to the arguments of the function. If the command name matches neither a Special Command nor the name of a defined function, a new process is created and an attempt is made to execute the command via exec(2).
The shell parameter PATH defines the search path for the directory containing the command. Alternative directory names are separated by a colon (:). The default path is /usr/bin. The current directory is specified by a null path name, which can appear immediately after the equal sign, between two colon delimiters anywhere in the path list, or at the end of the path list. If the command name contains a / the search path is not used. Otherwise, each directory in the path is searched for an executable file. If the file has execute permission but is not an a.out file, it is assumed to be a file containing shell commands. A sub-shell is spawned to read it. A parenthesized command is also executed in a sub-shell.
The location in the search path where a command was found is remembered by the shell (to help avoid unnecessary execs later). If the command was found in a relative directory, its location must be re-determined whenever the current directory changes. The shell forgets all remembered locations whenever the PATH variable is changed or the hash -r command is executed (see below).
Input/output redirection is now permitted for these commands. File descriptor 1 is the default output location. When Job Control is enabled, additional Special Commands are added to the shell's environment (see Job Control section below).
:
. filename
bg [%jobid ...]
break [ n ]
cd [ argument ]
chdir [ dir ]
continue [ n ]
echo [ arguments ... ]
eval [ argument ... ]
exec [ argument ... ]
exit [ n ]
export [ name ... ]
fg [%jobid ...]
getopts
hash [ -r ] [ name ... ]
jobs [-p|-l] [%jobid ...]
jobs -x command [arguments]
kill [ -sig ] %job ...
kill -l
login [ argument ... ]
newgrp [ argument ]
pwd
read name ...
readonly [ name ... ]
return [ n ]
set [ -aefhkntuvx [ argument ... ] ]
-a
-e
-f
-h
-k
-n
-t
-u
-v
-x
-
Using + rather than - causes these flags to be turned off. These flags can also be used upon invocation of the shell. The current set of flags can be found in $-. The remaining arguments are positional parameters and are assigned, in order, to $1, $2, ... If no arguments are given, the values of all names are printed.
shift [ n ]
stop pid ...
suspend
test
times
trap [ argument n [ n2 ... ]]
type [ name ... ]
ulimit [ [-HS] [-a | -cdfnstv] ]
ulimit [ [-HS] [-c | -d | -f | -n | -s | -t | -v] ] limit
If limit is not present, ulimit prints the specified limits. Any number of limits can be printed at one time. The -a option prints all limits.
If limit is present, ulimit sets the specified limit to limit. The string unlimited requests that the current limit, if any, be removed. Any user can set a soft limit to any value less than or equal to the hard limit. Any user can lower a hard limit. Only a user with appropriate privileges can raise or remove a hard limit. See getrlimit(2).
The -H option specifies a hard limit. The -S option specifies a soft limit. If neither option is specified, ulimit sets both limits and print the soft limit.
The following options specify the resource whose limits are to be printed or set. If no option is specified, the file size limit is printed or set.
-c
-d
-f
-n
-s
-t
-v
Run the sysdef(1M) command to obtain the maximum possible limits for your system. The values reported are in hexadecimal, but can be translated into decimal numbers using the bc(1) utility. See swap(1M).)
As an example of ulimit, to limit the size of a core file dump to 0 Megabytes, type the following:
ulimit -c 0
umask [ nnn ]
unset [ name ... ]
wait [ n ]
If the shell is invoked through exec(2) and the first character of argument zero is -, commands are initially read from /etc/profile and from $HOME/.profile, if such files exist. Thereafter, commands are read as described below, which is also the case when the shell is invoked as /usr/bin/sh. The flags below are interpreted by the shell on invocation only. Note: Unless the -c or -s flag is specified, the first argument is assumed to be the name of a file containing commands, and the remaining arguments are passed as positional parameters to that command file:
-c string
-i
-p
-r
-s
The remaining flags and arguments are described under the set command above.
When the shell is invoked as jsh, Job Control is enabled in addition to all of the functionality described previously for sh. Typically, Job Control is enabled for the interactive shell only. Non-interactive shells typically do not benefit from the added functionality of Job Control.
With Job Control enabled, every command or pipeline the user enters at the terminal is called a job. All jobs exist in one of the following states: foreground, background, or stopped. These terms are defined as follows:
Every job that the shell starts is assigned a positive integer, called a job number which is tracked by the shell and is used as an identifier to indicate a specific job. Additionally, the shell keeps track of the current and previous jobs. The current job is the most recent job to be started or restarted. The previous job is the first non-current job.
The acceptable syntax for a Job Identifier is of the form:
%jobid
where jobid can be specified in any of the following formats:
% or +
-
?<string>
n
pref
When Job Control is enabled, the following commands are added to the user's environment to manipulate jobs:
bg [%jobid ...]
fg [%jobid ...]
jobs [-p|-l] [%jobid ...]
jobs -x command [arguments]
-l
-p
-x
kill [ -signal ] %jobid
stop %jobid ...
suspend
wait [%jobid ...]
See largefile(5) for the description of the behavior of sh and jsh when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
Errors detected by the shell, such as syntax errors, cause the shell to return a non-zero exit status. If the shell is being used non-interactively execution of the shell file is abandoned. Otherwise, the shell returns the exit status of the last command executed (see also the exit command above).
If the shell is invoked as jsh and an attempt is made to exit the shell while there are stopped jobs, the shell issues one warning:
There are stopped jobs.
This is the only message. If another exit attempt is made, and there are still stopped jobs they are sent a SIGHUP signal from the kernel and the shell is exited.
$HOME/.profile
/dev/null
/etc/profile
See attributes(5) for descriptions of the following attributes:
|
|
Intro(1), bc(1), echo(1), getoptcvt(1), kill(1), ksh(1), login(1), newgrp(1), pfsh(1), pfexec(1), ps(1), pwd(1), set(1), shell_builtins(1), stty(1), test(1), umask(1), wait(1), rsh(1M), su(1M), swap(1M), sysdef(1M), dup(2), exec(2), fork(2), getrlimit(2), pipe(2), ulimit(2), setlocale(3C), signal.h(3HEAD), passwd(4), profile(4), attributes(5), environ(5), largefile(5), XPG4(5)
The use of setuid shell scripts is strongly discouraged.
Words used for filenames in input/output redirection are not interpreted for filename generation (see File Name Generation section above). For example, cat file1 >a* createsa file named a*.
Because commands in pipelines are run as separate processes, variables set in a pipeline have no effect on the parent shell.
If the input or the output of a while or until loop is redirected, the commands in the loop are run in a sub-shell, and variables set or changed there have no effect on the parent process:
lastline= while read line do lastline=$line done < /etc/passwd echo "lastline=$lastline" # lastline is empty!
In these cases, the input or output can be redirected by using exec, as in the following example:
# Save standard input (file descriptor 0) as file # descriptor 3, and redirect standard input from the file /etc/passwd: exec 3<&0 # save standard input as fd 3 exec </etc/passwd # redirect input from file lastline= while read line do lastline=$line done exec 0<&3 # restore standard input exec 3<&- # close file descriptor 3 echo "$lastline" # lastline
If you get the error message, "cannot fork, too many processes", try using the wait(1) command to clean up your background processes. If this doesn't help, the system process table is probably full or you have too many active foreground processes. There is a limit to the number of process ids associated with your login, and to the number the system can keep track of.
Only the last process in a pipeline can be waited for.
If a command is executed, and a command with the same name is installed in a directory in the search path before the directory where the original command was found, the shell continues to exec the original command. Use the hash command to correct this situation.
The Bourne shell has a limitation on the effective UID for a process. If this UID is less than 100 (and not equal to the real UID of the process), then the UID is reset to the real UID of the process.
Because the shell implements both foreground and background jobs in the same process group, they all receive the same signals, which can lead to unexpected behavior. It is, therefore, recommended that other job control shells be used, especially in an interactive environment.
When the shell executes a shell script that attempts to execute a non-existent command interpreter, the shell returns an erroneous diagnostic message that the shell script file does not exist.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |