📄 bashref.texi
字号:
* Comments:: How to specify comments.@end menuWhen the shell reads input, it proceeds through asequence of operations. If the input indicates the beginning of acomment, the shell ignores the comment symbol (@samp{#}), and the restof that line. Otherwise, roughly speaking, the shell reads its input anddivides the input into words and operators, employing the quoting rulesto select which meanings to assign various words and characters.The shell then parses these tokens into commands and other constructs,removes the special meaning of certain words or characters, expandsothers, redirects input and output as needed, executes the specifiedcommand, waits for the command's exit status, and makes that exit statusavailable for further inspection or processing.@node Shell Operation@subsection Shell OperationThe following is a brief description of the shell's operation when itreads and executes a command. Basically, the shell does thefollowing:@enumerate@itemReads its input from a file (@pxref{Shell Scripts}), from a stringsupplied as an argument to the @option{-c} invocation option(@pxref{Invoking Bash}), or from the user's terminal.@itemBreaks the input into words and operators, obeying the quoting rulesdescribed in @ref{Quoting}. These tokens are separated by@code{metacharacters}. Alias expansion is performed by this step(@pxref{Aliases}).@itemParses the tokens into simple and compound commands(@pxref{Shell Commands}).@itemPerforms the various shell expansions (@pxref{Shell Expansions}), breakingthe expanded tokens into lists of filenames (@pxref{Filename Expansion})and commands and arguments.@itemPerforms any necessary redirections (@pxref{Redirections}) and removesthe redirection operators and their operands from the argument list.@itemExecutes the command (@pxref{Executing Commands}).@itemOptionally waits for the command to complete and collects its exitstatus (@pxref{Exit Status}).@end enumerate@node Quoting@subsection Quoting@cindex quoting@menu* Escape Character:: How to remove the special meaning from a single character.* Single Quotes:: How to inhibit all interpretation of a sequence of characters.* Double Quotes:: How to suppress most of the interpretation of a sequence of characters.* ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings.* Locale Translation:: How to translate strings into different languages.@end menuQuoting is used to remove the special meaning of certaincharacters or words to the shell. Quoting can be used todisable special treatment for special characters, to preventreserved words from being recognized as such, and to preventparameter expansion.Each of the shell metacharacters (@pxref{Definitions})has special meaning to the shell and must be quoted if it is torepresent itself.When the command history expansion facilities are being used(@pxref{History Interaction}), the@var{history expansion} character, usually @samp{!}, must be quotedto prevent history expansion. @xref{Bash History Facilities}, formore details concerning history expansion.There are three quoting mechanisms: the@var{escape character}, single quotes, and double quotes.@node Escape Character@subsubsection Escape CharacterA non-quoted backslash @samp{\} is the Bash escape character.It preserves the literal value of the next character that follows,with the exception of @code{newline}. If a @code{\newline} pairappears, and the backslash itself is not quoted, the @code{\newline}is treated as a line continuation (that is, it is removed fromthe input stream and effectively ignored).@node Single Quotes@subsubsection Single QuotesEnclosing characters in single quotes (@samp{'}) preserves the literal valueof each character within the quotes. A single quote may not occurbetween single quotes, even when preceded by a backslash.@node Double Quotes@subsubsection Double QuotesEnclosing characters in double quotes (@samp{"}) preserves the literal valueof all characters within the quotes, with the exception of@samp{$}, @samp{`}, @samp{\},and, when history expansion is enabled, @samp{!}.The characters @samp{$} and @samp{`}retain their special meaning within double quotes (@pxref{Shell Expansions}).The backslash retains its special meaning only when followed by one ofthe following characters:@samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}.Within double quotes, backslashes that are followed by one of thesecharacters are removed. Backslashes preceding characters without aspecial meaning are left unmodified.A double quote may be quoted within double quotes by preceding it witha backslash.If enabled, history expansion will be performed unless an @samp{!}appearing in double quotes is escaped using a backslash.The backslash preceding the @samp{!} is not removed.The special parameters @samp{*} and @samp{@@} have special meaningwhen in double quotes (@pxref{Shell Parameter Expansion}).@node ANSI-C Quoting@subsubsection ANSI-C Quoting@cindex quoting, ANSIWords of the form @code{$'@var{string}'} are treated specially. Theword expands to @var{string}, with backslash-escaped characters replacedas specified by the ANSI C standard. Backslash escape sequences, ifpresent, are decoded as follows:@table @code@item \aalert (bell)@item \bbackspace@item \e@itemx \Ean escape character (not ANSI C)@item \fform feed@item \nnewline@item \rcarriage return@item \thorizontal tab@item \vvertical tab@item \\backslash@item \'single quote@item \"double quote@item \@var{nnn}the eight-bit character whose value is the octal value @var{nnn}(one to three digits)@item \x@var{HH}the eight-bit character whose value is the hexadecimal value @var{HH}(one or two hex digits)@item \u@var{HHHH}the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value@var{HHHH} (one to four hex digits)@item \U@var{HHHHHHHH}the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value@var{HHHHHHHH} (one to eight hex digits)@item \c@var{x}a control-@var{x} character@end table@noindentThe expanded result is single-quoted, as if the dollar sign had notbeen present.@node Locale Translation@subsubsection Locale-Specific Translation@cindex localization@cindex internationalization@cindex native languages@cindex translation, native languagesA double-quoted string preceded by a dollar sign (@samp{$}) will causethe string to be translated according to the current locale.If the current locale is @code{C} or @code{POSIX}, the dollar signis ignored.If the string is translated and replaced, the replacement isdouble-quoted.@vindex LC_MESSAGES@vindex TEXTDOMAIN@vindex TEXTDOMAINDIRSome systems use the message catalog selected by the @env{LC_MESSAGES}shell variable. Others create the name of the message catalog from thevalue of the @env{TEXTDOMAIN} shell variable, possibly adding asuffix of @samp{.mo}. If you use the @env{TEXTDOMAIN} variable, youmay need to set the @env{TEXTDOMAINDIR} variable to the location ofthe message catalog files. Still others use both variables in thisfashion:@env{TEXTDOMAINDIR}/@env{LC_MESSAGES}/LC_MESSAGES/@env{TEXTDOMAIN}.mo.@node Comments@subsection Comments@cindex comments, shellIn a non-interactive shell, or an interactive shell in which the@code{interactive_comments} option to the @code{shopt}builtin is enabled (@pxref{The Shopt Builtin}),a word beginning with @samp{#}causes that word and all remaining characters on that line tobe ignored. An interactive shell without the @code{interactive_comments}option enabled does not allow comments. The @code{interactive_comments}option is on by default in interactive shells.@xref{Interactive Shells}, for a description of what makesa shell interactive.@node Shell Commands@section Shell Commands@cindex commands, shellA simple shell command such as @code{echo a b c} consists of the commanditself followed by arguments, separated by spaces.More complex shell commands are composed of simple commands arranged togetherin a variety of ways: in a pipeline in which the output of one commandbecomes the input of a second, in a loop or conditional construct, or insome other grouping.@menu* Simple Commands:: The most common type of command.* Pipelines:: Connecting the input and output of several commands.* Lists:: How to execute commands sequentially.* Compound Commands:: Shell commands for control flow.* Coprocesses:: Two-way communication between commands.* GNU Parallel:: Running commands in parallel.@end menu@node Simple Commands@subsection Simple Commands@cindex commands, simpleA simple command is the kind of command encountered most often.It's just a sequence of words separated by @code{blank}s, terminatedby one of the shell's control operators (@pxref{Definitions}). Thefirst word generally specifies a command to be executed, with therest of the words being that command's arguments.The return status (@pxref{Exit Status}) of a simple command isits exit status as providedby the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} ifthe command was terminated by signal @var{n}.@node Pipelines@subsection Pipelines@cindex pipeline@cindex commands, pipelinesA @code{pipeline} is a sequence of simple commands separated by one ofthe control operators @samp{|} or @samp{|&}.@rwindex time@rwindex !@cindex command timingThe format for a pipeline is@example[@code{time} [@code{-p}]] [@code{!}] @var{command1} [ [@code{|} or @code{|&}] @var{command2} @dots{}]@end example@noindentThe output of each command in the pipeline is connected via a pipeto the input of the next command.That is, each command reads the previous command's output. Thisconnection is performed before any redirections specified by thecommand.If @samp{|&} is used, the standard error of @var{command1} is connected to@var{command2}'s standard input through the pipe; it is shorthand for@code{2>&1 |}. This implicit redirection of the standard error isperformed after any redirections specified by the command.The reserved word @code{time} causes timing statisticsto be printed for the pipeline once it finishes.The statistics currently consist of elapsed (wall-clock) time anduser and system time consumed by the command's execution.The @option{-p} option changes the output format to that specifiedby @sc{posix}.When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),it does not recognize @code{time} as a reserved word if the nexttoken begins with a @samp{-}.The @env{TIMEFORMAT} variable may be set to a format string thatspecifies how the timing information should be displayed.@xref{Bash Variables}, for a description of the available formats.The use of @code{time} as a reserved word permits the timing ofshell builtins, shell functions, and pipelines. An external@code{time} command cannot time these easily.When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time}may be followed by a newline. In this case, the shell displays thetotal user and system time consumed by the shell and its children.The @env{TIMEFORMAT} variable may be used to specify the format ofthe time information.If the pipeline is not executed asynchronously (@pxref{Lists}), theshell waits for all commands in the pipeline to complete.Each command in a pipeline is executed in its own subshell(@pxref{Command Execution Environment}). The exitstatus of a pipeline is the exit status of the last command in thepipeline, unless the @code{pipefail} option is enabled(@pxref{The Set Builtin}).If @code{pipefail} is enabled, the pipeline's return status is thevalue of the last (rightmost) command to exit with a non-zero status,or zero if all commands exit successfully.If the reserved word @samp{!} precedes the pipeline, theexit status is the logical negation of the exit status as describedabove.The shell waits for all commands in the pipeline to terminate beforereturning a value.@node Lists@subsection Lists of Commands@cindex commands, listsA @code{list} is a sequence of one or more pipelines separated by oneof the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},and optionally terminated by one of @samp{;}, @samp{&}, or a@code{newline}.Of these list operators, @samp{&&} and @samp{||}have equal precedence, followed by @samp{;} and @samp{&},which have equal precedence.A sequence of one or more newlines may appear in a @code{list}to delimit commands, equivalent to a semicolon.If a command is terminated by the control operator @samp{&},the shell executes the command asynchronously in a subshell.This is known as executing the command in the @var{background}.The shell does not wait for the command to finish, and the returnstatus is 0 (true).When job control is not active (@pxref{Job Control}),the standard input for asynchronous commands, in the absence of anyexplicit redirections, is redirected from @code{/dev/null}.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -