📄 bashref.texi
字号:
Commands separated by a @samp{;} are executed sequentially; the shellwaits for each command to terminate in turn. The return status is theexit status of the last command executed.@sc{and} and @sc{or} lists are sequences of one or more pipelinesseparated by the control operators @samp{&&} and @samp{||},respectively. @sc{and} and @sc{or} lists are executed with leftassociativity.An @sc{and} list has the form@example@var{command1} && @var{command2}@end example@noindent@var{command2} is executed if, and only if, @var{command1}returns an exit status of zero.An @sc{or} list has the form@example@var{command1} || @var{command2}@end example@noindent@var{command2} is executed if, and only if, @var{command1}returns a non-zero exit status.The return status of@sc{and} and @sc{or} lists is the exit status of the last commandexecuted in the list.@node Compound Commands@subsection Compound Commands@cindex commands, compound@menu* Looping Constructs:: Shell commands for iterative action.* Conditional Constructs:: Shell commands for conditional execution.* Command Grouping:: Ways to group commands.@end menuCompound commands are the shell programming constructs.Each construct begins with a reserved word or control operator and isterminated by a corresponding reserved word or operator.Any redirections (@pxref{Redirections}) associated with a compound commandapply to all commands within that compound command unless explicitly overridden.Bash provides looping constructs, conditional commands, and mechanismsto group commands and execute them as a unit.@node Looping Constructs@subsubsection Looping Constructs@cindex commands, loopingBash supports the following looping constructs.Note that wherever a @samp{;} appears in the description of acommand's syntax, it may be replaced with one or more newlines.@table @code@item until@rwindex until@rwindex do@rwindex doneThe syntax of the @code{until} command is:@exampleuntil @var{test-commands}; do @var{consequent-commands}; done@end exampleExecute @var{consequent-commands} as long as@var{test-commands} has an exit status which is not zero.The return status is the exit status of the last command executedin @var{consequent-commands}, or zero if none was executed.@item while@rwindex whileThe syntax of the @code{while} command is:@examplewhile @var{test-commands}; do @var{consequent-commands}; done@end exampleExecute @var{consequent-commands} as long as@var{test-commands} has an exit status of zero.The return status is the exit status of the last command executedin @var{consequent-commands}, or zero if none was executed.@item for@rwindex forThe syntax of the @code{for} command is:@examplefor @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done@end exampleExpand @var{words}, and execute @var{commands} once for each memberin the resultant list, with @var{name} bound to the current member.If @samp{in @var{words}} is not present, the @code{for} commandexecutes the @var{commands} once for each positional parameter that isset, as if @samp{in "$@@"} had been specified(@pxref{Special Parameters}).The return status is the exit status of the last command that executes.If there are no items in the expansion of @var{words}, no commands areexecuted, and the return status is zero.An alternate form of the @code{for} command is also supported:@examplefor (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done@end exampleFirst, the arithmetic expression @var{expr1} is evaluated accordingto the rules described below (@pxref{Shell Arithmetic}).The arithmetic expression @var{expr2} is then evaluated repeatedlyuntil it evaluates to zero. Each time @var{expr2} evaluates to a non-zero value, @var{commands} areexecuted and the arithmetic expression @var{expr3} is evaluated. If any expression is omitted, it behaves as if it evaluates to 1.The return value is the exit status of the last command in @var{commands}that is executed, or false if any of the expressions is invalid.@end tableThe @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})may be used to control loop execution.@node Conditional Constructs@subsubsection Conditional Constructs@cindex commands, conditional@table @code@item if@rwindex if@rwindex then@rwindex else@rwindex elif@rwindex fiThe syntax of the @code{if} command is:@exampleif @var{test-commands}; then @var{consequent-commands};[elif @var{more-test-commands}; then @var{more-consequents};][else @var{alternate-consequents};]fi@end exampleThe @var{test-commands} list is executed, and if its return status is zero,the @var{consequent-commands} list is executed.If @var{test-commands} returns a non-zero status, each @code{elif} listis executed in turn, and if its exit status is zero,the corresponding @var{more-consequents} is executed and the command completes.If @samp{else @var{alternate-consequents}} is present, andthe final command in the final @code{if} or @code{elif} clausehas a non-zero exit status, then @var{alternate-consequents} is executed.The return status is the exit status of the last command executed, orzero if no condition tested true.@item case@rwindex case@rwindex in@rwindex esacThe syntax of the @code{case} command is:@example@code{case @var{word} in [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{} esac}@end example@code{case} will selectively execute the @var{command-list} corresponding tothe first @var{pattern} that matches @var{word}.If the shell option @code{nocasematch}(see the description of @code{shopt} in @ref{The Shopt Builtin})is enabled, the match is performed without regard to the caseof alphabetic characters.The @samp{|} is used to separate multiple patterns, and the @samp{)}operator terminates a pattern list.A list of patterns and an associated command-list is knownas a @var{clause}.Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}.The @var{word} undergoes tilde expansion, parameter expansion, commandsubstitution, arithmetic expansion, and quote removal before matching isattempted. Each @var{pattern} undergoes tilde expansion, parameterexpansion, command substitution, and arithmetic expansion.There may be an arbitrary number of @code{case} clauses, each terminatedby a @samp{;;}, @samp{;&}, or @samp{;;&}.The first pattern that matches determines thecommand-list that is executed.Here is an example using @code{case} in a script that could be used todescribe one interesting feature of an animal:@exampleecho -n "Enter the name of an animal: "read ANIMALecho -n "The $ANIMAL has "case $ANIMAL in horse | dog | cat) echo -n "four";; man | kangaroo ) echo -n "two";; *) echo -n "an unknown number of";;esacecho " legs."@end example@noindentIf the @samp{;;} operator is used, no subsequent matches are attempted afterthe first pattern match.Using @samp{;&} in place of @samp{;;} causes execution to continue withthe @var{command-list} associated with the next clause, if any.Using @samp{;;&} in place of @samp{;;} causes the shell to test the patternsin the next clause, if any, and execute any associated @var{command-list}on a successful match.The return status is zero if no @var{pattern} is matched. Otherwise, thereturn status is the exit status of the @var{command-list} executed.@item select@rwindex selectThe @code{select} construct allows the easy generation of menus.It has almost the same syntax as the @code{for} command:@exampleselect @var{name} [in @var{words} @dots{}]; do @var{commands}; done@end exampleThe list of words following @code{in} is expanded, generating a listof items. The set of expanded words is printed on the standarderror output stream, each preceded by a number. If the@samp{in @var{words}} is omitted, the positional parameters are printed,as if @samp{in "$@@"} had been specified.The @env{PS3} prompt is then displayed and a line is read from thestandard input.If the line consists of a number corresponding to one of the displayedwords, then the value of @var{name} is set to that word.If the line is empty, the words and prompt are displayed again.If @code{EOF} is read, the @code{select} command completes.Any other value read causes @var{name} to be set to null.The line read is saved in the variable @env{REPLY}.The @var{commands} are executed after each selection until a@code{break} command is executed, at whichpoint the @code{select} command completes.Here is an example that allows the user to pick a filename from thecurrent directory, and displays the name and index of the fileselected.@exampleselect fname in *;do echo you picked $fname \($REPLY\) break;done@end example@item ((@dots{}))@example(( @var{expression} ))@end exampleThe arithmetic @var{expression} is evaluated according to the rulesdescribed below (@pxref{Shell Arithmetic}).If the value of the expression is non-zero, the return status is 0;otherwise the return status is 1. This is exactly equivalent to@examplelet "@var{expression}"@end example@noindent@xref{Bash Builtins}, for a full description of the @code{let} builtin.@item [[@dots{}]]@rwindex [[@rwindex ]]@example[[ @var{expression} ]]@end exampleReturn a status of 0 or 1 depending on the evaluation ofthe conditional expression @var{expression}.Expressions are composed of the primaries described below in@ref{Bash Conditional Expressions}.Word splitting and filename expansion are not performed on the wordsbetween the @samp{[[} and @samp{]]}; tilde expansion, parameter andvariable expansion, arithmetic expansion, command substitution, processsubstitution, and quote removal are performed.Conditional operators such as @samp{-f} must be unquoted to be recognizedas primaries.When used with @samp{[[}, the @samp{<} and @samp{>} operators sortlexicographically using the current locale.When the @samp{==} and @samp{!=} operators are used, the string to theright of the operator is considered a pattern and matched accordingto the rules described below in @ref{Pattern Matching}.If the shell option @code{nocasematch}(see the description of @code{shopt} in @ref{The Shopt Builtin})is enabled, the match is performed without regard to the caseof alphabetic characters.The return value is 0 if the string matches (@samp{==}) or does notmatch (@samp{!=})the pattern, and 1 otherwise.Any part of the pattern may be quoted to force it to be matched as astring.An additional binary operator, @samp{=~}, is available, with the sameprecedence as @samp{==} and @samp{!=}.When it is used, the string to the right of the operator is consideredan extended regular expression and matched accordingly (as in @i{regex}3)). The return value is 0 if the string matchesthe pattern, and 1 otherwise.If the regular expression is syntactically incorrect, the conditionalexpression's return value is 2.If the shell option @code{nocasematch}(see the description of @code{shopt} in @ref{The Shopt Builtin})is enabled, the match is performed without regard to the caseof alphabetic characters.Any part of the pattern may be quoted to force it to be matched as astring.Substrings matched by parenthesized subexpressions within the regularexpression are saved in the array variable @code{BASH_REMATCH}.The element of @code{BASH_REMATCH} with index 0 is the portion of the stringmatching the entire regular expression.The element of @code{BASH_REMATCH} with index @var{n} is the portion of thestring matching the @var{n}th parenthesized subexpression.Expressions may be combined using the following operators, listedin decreasing order of precedence:@table @code@item ( @var{expression} )Returns the value of @var{expression}.This may be used to override the normal precedence of operators.@item ! @var{expression}True if @var{expression} is false.@item @var{expression1} && @var{expression2}True if both @var{expression1} and @var{expression2} are true.@item @var{expression1} || @var{expression2}True if either @var{expression1} or @var{expression2} is true.@end table@noindentThe @code{&&} and @code{||} operators do not evaluate @var{expression2} if thevalue of @var{expression1} is sufficient to determine the returnvalue of the entire conditional expression.@end table@node Command Grouping@subsubsection Grouping Commands@cindex commands, groupingBash provides two ways to group a list of commands to be executed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -