📄 sh.1
字号:
directory name.The current directorymay be indicated by an empty directory name..LPCommand names containing a slash are simply executed without performing any of the above searches..sp 2.B Command Exit Status.sp.LPEach command has an exit status that can influence the behaviorof other shell commands. The paradigm is that a command exitswith zero for normal or success, and non-zero for failure,error, or a false indication. The man page for each commandshould indicate the various exit codes and what they mean.Additionally, the builtin commands return exit codes, as doesan executed function..sp 2.B Complex Commands.sp.LPComplex commands are combinations of simple commandswith control operators or reserved words, together creating a larger complexcommand. More generally, a command is one of the following:.nf - simple command - pipeline - list or compound-list - compound command - function definition.fi.LPUnless otherwise stated, the exit status of a command isthat of the last simple command executed by the command..sp 2.B Pipeline.sp.LPA pipeline is a sequence of one or more commands separatedby the control operator |. The standard output of all butthe last command is connected to the standard inputof the next command..LPThe format for a pipeline is:.nf[!] command1 [ | command2 ...].fi.LPThe standard output of command1 is connected to the standardinput of command2. The standard input, standard output, orboth of a command is considered to be assigned by thepipeline before any redirection specified by redirectionoperators that are part of the command..LPIf the pipeline is not in the background (discussed later),the shell waits for all commands to complete..LPIf the reserved word ! does not precede the pipeline, theexit status is the exit status of the last command specifiedin the pipeline. Otherwise, the exit status is the logicalNOT of the exit status of the last command. That is, ifthe last command returns zero, the exit status is 1; ifthe last command returns greater than zero, the exit statusis zero..LPBecause pipeline assignment of standard input or standardoutput or both takes place before redirection, it can bemodified by redirection. For example:.nf$ command1 2>&1 | command2.fisends both the standard output and standard error of command1to the standard input of command2..LPA ; or <newline> terminator causes the precedingAND-OR-list (described next) to be executed sequentially; a & causesasynchronous execution of the preceding AND-OR-list..sp 2.B Background Commands -- &.sp.LPIf a command is terminated by the control operator ampersand(&), the shell executes the command asynchronously -- that is,the shell does not wait forthe command to finish before executing the next command..LPThe format for running a command in background is:.nfcommand1 & [command2 & ...].fiIf the shell is not interactive, the standard input of anasynchronous command is set to /dev/null..sp 2.B Lists -- Generally Speaking.sp.LPA list is a sequence of zero or more commands separated bynewlines, semicolons, or ampersands,and optionally terminated by one of these three characters.The commands in alist are executed in the order they are written.If command is followed by an ampersand, the shell starts thecommand and immediately proceed onto the next command;otherwise it waits for the command to terminate beforeproceeding to the next one..LP``&&'' and ``||'' are AND-OR list operators. ``&&'' executesthe first command, and then executes the second commandiff the exit status of the first command is zero. ``||''is similar, but executes the second command iff the exitstatus of the first command is nonzero. ``&&'' and ``||''both have the same priority..LPThe syntax of the if command is.nf if list then list [ elif list then list ] ... [ else list ] fi.fiThe syntax of the while command is.nf while list do list done.fiThe 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 whilerepeat until the exit status of the first list is zero..LPThe syntax of the for command is.nf for variable in word... do list done.fiThe words are expanded, and then the list is executedrepeatedly with the variable set to each word in turn. doand done may be replaced with ``{'' and ``}''..LPThe syntax of the break and continue command is.nf break [ num ] continue [ num ].fiBreak terminates the num innermost for or while loops.Continue continues with the next iteration of the innermost loop. These are implemented as builtin commands..LPThe syntax of the case command is.nf case word in pattern) list ;; ... esac.fi.LPThe pattern can actually be one or more patterns (see ShellPatterns described later), separated by ``|'' characters..LPCommands may be grouped by writing either.nf (list).fior.nf { list; }.fiThe first of these executes the commands in a subshell..sp 2.B Functions.sp.LPThe syntax of a function definition is.nf name ( ) command.fi.LPA function definition is an executable statement; whenexecuted it installs a function named name and returns anexit status of zero. The command is normally a listenclosed between ``{'' and ``}''..LPVariables may be declared to be local to a function byusing a local command. This should appear as the firststatement of a function, and the syntax is.nf local [ variable | - ] ....fiLocal is implemented as a builtin command..LPWhen a variable is made local, it inherits the initialvalue and exported and readonly flags from the variablewith the same name in the surrounding scope, if there isone. Otherwise, the variable is initially unset. The shelluses dynamic scoping, so that if you make the variable xlocal to function f, which then calls function g, references to the variable x made inside g will refer to thevariable x declared inside f, not to the global variablenamed x..LPThe only special parameter than can be made local is``-''. Making ``-'' local any shell options that arechanged via the set command inside the function to berestored to their original values when the functionreturns..LPThe syntax of the return command is.nf return [ exitstatus ].fiIt terminates the currently executing function. Return isimplemented as a builtin command..sp 2.B Variables and Parameters.sp.LPThe shell maintains a set of parameters. A parameterdenoted by a name is called a variable. When starting up,the shell turns all the environment variables into shellvariables. New variables can be set using the form.nf name=value.fi.LPVariables set by the user must have a name consisting solelyof alphabetics, numerics, and underscores - the first of whichmust not be numeric. A parameter can also be denoted by a numberor a special character as explained below..sp 2.B Positional Parameters.sp.LPA positional parameter is a parameter denoted by a number (n > 0).The shell sets these initially to the values of its commandline arguments that follow the name of the shell script.The set(1) builtin can also be used to set or reset them..sp 2.B Special Parameters.sp.LPA special parameter is a parameter denoted by one of the followingspecial characters. The value of the parameter is listednext to its character..TP*Expands to the positional parameters, starting from one. Whenthe expansion occurs within a double-quoted stringit expands to a single field with the value of each parameterseparated by the first character of the IFS variable, or by a<space> if IFS is unset..TP@Expands to the positional parameters, starting from one. Whenthe expansion occurs within double-quotes, each positionalparameter expands as a separate argument.If there are no positional parameters, theexpansion of @ generates zero arguments, even when @ isdouble-quoted. What this basically means, for example, isif $1 is ``abc'' and $2 is ``def ghi'', then "$@" expands tothe two arguments:"abc" "def ghi".TP#Expands to the number of positional parameters..TP?Expands to the exit status of the most recent pipeline..TP- (Hyphen)Expands to the current option flags (the single-letteroption names concatenated into a string) as specified oninvocation, by the set builtin command, or implicitlyby the shell..TP$Expands to the process ID of the invoked shell. A subshellretains the same value of $ as its parent..TP!Expands to the process ID of the most recent backgroundcommand executed from the current shell. For apipeline, the process ID is that of the last command in thepipeline..TP0 (Zero.)Expands to the name of the shell or shell script..LP.sp 2.B Word Expansions.sp.LPThis clause describes the various expansions that areperformed on words. Not all expansions are performed onevery word, as explained later..LPTilde expansions, parameter expansions, command substitutions,arithmetic expansions, and quote removals that occur withina single word expand to a single field. It is only fieldsplitting or pathname expansion that can create multiplefields from a single word. The single exception to thisrule is the expansion of the special parameter @ withindouble-quotes, as was described above..LPThe order of word expansion is:.LP(1) Tilde Expansion, Parameter Expansion, Command Substitution,Arithmetic Expansion (these all occur at the same time)..LP(2) Field Splitting is performed on fieldsgenerated by step (1) unless the IFS variable is null..LP(3) Pathname Expansion (unless set -f is in effect)..LP(4) Quote Removal..LPThe $ character is used to introduce parameter expansion, commandsubstitution, or arithmetic evaluation..sp 2.B Tilde Expansion (substituting a user's home directory).sp .LPA word beginning with an unquoted tilde character (~) issubjected to tilde expansion. All the characters up toa slash (/) or the end of the word are treated as a usernameand are replaced with the user's home directory. If theusername is missing (as in ~/foobar), the tilde is replacedwith the value of the HOME variable (the current user'shome directory)..sp 2.B Parameter Expansion.sp.LPThe format for parameter expansion is as follows:.nf ${expression}.fiwhere expression consists of all characters until the matching }. Any }escaped by a backslash or within a quoted string, and characters inembedded arithmetic expansions, command substitutions, and variableexpansions, are not examined in determining the matching }..LPThe simplest form for parameter expansion is:.nf ${parameter}.fiThe value, if any, of parameter is substituted..LPThe parameter name or symbol can be enclosed in braces, which areoptional except for positional parameters with more than one digit orwhen parameter is followed by a character that could be interpreted aspart of the name.If a parameter expansion occurs insidedouble-quotes:.LP1) Pathname expansion is not performed on the results of theexpansion..LP2) Field splitting is not performed on the results of theexpansion, with the exception of @..LPIn addition, a parameter expansion can be modified by using one of thefollowing formats..sp.TP${parameter:-word}Use Default Values. If parameter is unset ornull, the expansion of word issubstituted; otherwise, the value ofparameter is substituted..TP${parameter:=word}Assign Default Values. If parameter is unsetor null, the expansion of word isassigned to parameter. In all cases, thefinal value of parameter issubstituted. Only variables, not positionalparameters or special parameters, can beassigned in this way..TP${parameter:?[word]}Indicate Error if Null or Unset. Ifparameter is unset or null, the expansion ofword (or a message indicating it is unset ifword is omitted) is written to standarderror and the shell exits with a nonzeroexit status. Otherwise, the value ofparameter is substituted. Aninteractive shell need not exit..TP${parameter:+word}Use Alternate Value. If parameter is unsetor null, null is substituted;otherwise, the expansion of word issubstituted..LPIn the parameter expansions shown previously, use of the colon in the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -