bash - GNU Bourne-Again SHell

Overview (SYNOPSIS)

bash [options] [file]

Copyright (COPYRIGHT)

Bash is Copyright (C) 1989-2002 by the Free Software Foundation, Inc.

Description (DESCRIPTION)

Bash is with a sh -compatible command language interpreter, the command can be executed or read from the standard input file. Bash also incorporates Korn and C Shell ( ksh and csh outstanding characteristics) in.

Bash goal is to be achieved following the IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2) a.

Options (OPTIONS)

In addition to the set to single-character options (option) is about the document's built-in commands, bash when starting explains the following options.

 

-c string
If there -c option, the command from the string read. If the string has a parameter (argument) later, they will be used to position parameter (positional parameter, to $ 0 start) assignment.
-i
If there is -i option, shell will be executed interactively ( Interactive ).
-l
Option causes bash in a similar login shell (login shell) way to start (see below start (INVOCATION) chapter).
-r
If there -r option, shell becomes restricted ( Tel Restricted ) (see the following restricted shell (RESTRICTED SHELL) chapter).
-s
If there -s option after option processing, or if no arguments remain then commands are read from standard input. This option allows when you start an interactive shell positional parameters can be set.
-D
Printed to standard output in a $ as the lead, a list of double-quoted strings. This is the current locale is not C or POSIX string, the script to be translated (?). This option includes the -n option; not execute the command.
[-+]O [ shopt_option]
shopt_option is a shopt builtin acceptable option (see below shell builtin (SHELL BUILTIN COMMANDS) section). If there shopt_option , -O sets the value of that option; + O to cancel it. If no shopt_option , the shopt name and value of the acceptable options will be printed on the standard output. If the startup option is + O , output as a display format can be re-used as input.
--
- notify the end of options and disables further option processing. Any - parameters after the file name and will be treated as a parameter. This is quite a specified character is - a parameter (?).

Bash also explain some multi-byte option. At the command line, these options must be placed before a single character parameters need to be identified.

 

--dump-po-strings
Equivalent to -D , but the output is the GNU the gettext PO (Portable Object) file format
--dump-strings
Equivalent to -D
--help
Displays usage information on standard output and exit successfully
--init-file file
--rcfile file
If the shell is interactive, perform file personal initialization commands in the file, instead of the standard ~ / .bashrc (see the following start (INVOCATION) section)
--login
Equivalent to -l
--noediting
If the shell is interactive, do not use the GNU readline library to read command line
--noprofile
Not read range of the system startup file / etc / profile or any personal initialization files ~ / .bash_profile , ~ / .bash_login , or ~ / .profile . By default, bash reads these files when you start as a login shell (see the following start (INVOCATION) section)
--norc
If the shell is interactive, not read / execute the personal initialization file ~ / .bashrc This option shell to sh start command is turned on by default
--posix
If the default action and POSIX 1003.2 standard different things, changing bash behavior to meet the standards ( the POSIX the MODE )
--restricted
shell becomes restricted (see the following restricted shell (RESTRICTED SHELL) section)
--rpm-requires
In order to produce a script that runs a list of files, needed. This option includes the -n option. It is to avoid errors when limit detection compile - Backticks, [] tests, as well as evals not be explained, some of the dependencies may be lost
--verbose
Equivalent to -v
--version
In the standard output this bash version information and exit successfully.

Parameters (ARGUMENTS)

After treatment option if arguments remain, and neither the -c or -s option, the first argument will be assumed name for a file containing shell commands. If bash is started in this way, the $ 0 is set to the name of the file, the positional parameters are set to the remaining other parameters. Bash read from this file and execute the command, then exits. Bash exit status is the exit status of the last command executed in the script. If no commands are executed, the exit status is 0. Step to try is to try to open the file in the current directory, then, if not found, shell script will search PATH path environment variable.

Start (INVOCATION)

login shell login shell, is the first character of argument zero is a - , or one started with the --login shell options.

Interactive the shell interaction, there is no specified non-option parameters a start, is not specified -c option, the standard output and standard inputs are connected to the terminal ( isatty (. 3) in the determination) of the shell, or boot is specified - i shell options. If bash is interactive, PS1 environment variable will be set and $ - includes i , allows a shell script or a startup file to test this state.

The following paragraphs describe the bash how to perform its startup files. If any of the files exist but one can not read, bash reports an error. File name tilde (~, tilde) as the EXPANSION section Expansion Tilde as described in paragraph deployed.

When bash is started as an interactive login shell, or a non-interactive shell specified --login option, it first reads and executes the / etc / profile in order, as long as the file exists. After reading the file, it looks in the following order ~ / .bash_profile , ~ / .bash_login , and ~ / .profile , and reads and executes commands from the first file exists and is readable. --noprofile option can be used to stop it from doing so when the shell is started.

When a login shell exits, bash reads and executes the file ~ / .bash_logout commands, as long as it exists.

When an interactive shell is not a login shell is started, bash from the file ~ / .bashrc reads and executes commands, as long as it exists. Can --norc option to prevent it from doing so. --rcfile file option will force bash to read and execute the file file commands instead of ~ / .bashrc in.

When bash when you start a non-interactive mode, for example, when you run a shell script, it looks in the environment variable BASH_ENV , if it exists, its value will expand the use of the expanded value as the name of a file to read and execute . Bash course of the operation just as the implementation of the following commands:

 

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

But do not use PATH variable's value to search for the file name.

If bash the name sh , it tries to imitate (the MIMIC) sh historical versions of the startup process, as similar as possible, while conforming to the POSIX standard.

When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

When bash is started in posix mode, as with the --posix command line option, it follows the POSIX standard for startup files. In this mode, interactive shells expand the ENV variable and commands are read and executed from the file whose name is the expanded value. No other startup files are read.

Bash attempts to determine when it is being run by the remote shell daemon, usually rshd. If bash determines it is being run by rshd, it reads and executes commands from ~/.bashrc, if that file exists and is readable. It will not do this if invoked as sh. The --norc option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or allow them to be specified.

If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS variable, if it appears in the environment, is ignored, and the effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.

Define (DEFINITIONS)

The following definitions are used throughout the rest of this document.

blank
A space or tab.
word
A sequence of characters considered as a single unit by the shell. Also known as a token.
name
A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier.
metacharacter
A character that, when unquoted, separates words. One of the following:

| & ; ( ) < > space tab

 

control operator
A token that performs a control function. It is one of the following symbols:

|| & && ; ;; ( ) | <newline>

Reserved words (RESERVED WORDS)

Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command:

! case do done elif else esac fi for function if in select then until while { } time [[ ]]

shell语法(SHELL GRAMMAR)

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

The return value of a simple command is its exit status, or 128+n if the command is terminated by signal n.

Pipelines

A pipeline is a sequence of one or more commands separated by the character |. The format for a pipeline is:

[time [-p]] [ ! ] command [ | command2 ... ]

The standard output of command is connected via a pipe to the standard input of command2. This connection is performed before any redirections specified by the command (see REDIRECTION below).

If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical NOT of the exit status of the last command. Otherwise, the status of the pipeline is the exit status of the last command. The shell waits for all commands in the pipeline to terminate before returning a value.

If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates. The -p option changes the output format to that specified by POSIX. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed; see the description of TIMEFORMAT under Shell Variables below.

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

Lists

A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or <newline>.

Of these list operators, && and || have equal precedence, followed by ; and &, which have equal precedence.

A sequence of one or more newlines may appear in a list instead of a semicolon to delimit commands.

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

The control operators && and || denote AND lists and OR lists, respectively. An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form

command1 || command2

 

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.

Compound Commands

A compound command is one of the following:

( list)
list is executed in a subshell. Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.
{ list; }
list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace.
(( expression))
The expression is evaluated according to the rules described below under ARITHMETICEVALUATION. 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 let " expression".
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONALEXPRESSIONS. Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.

When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching. The return value is 0 if the string matches or does not match the pattern, respectively, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string.

Expressions may be combined using the following operators, listed in decreasing order of precedence:

 

( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.
! expression
True if expression is false.
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2 True if either expression1 or expression2 is true.

The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

for name [ in word ]; do list ; done
The list of words following in is expanded, generating a list of items. The variable name is set to each element of this list in turn, and list is executed each time. If the in word is omitted, the for command executes list once for each positional parameter that is set (see PARAMETERS below). The return status is the exit status of the last command that executes. If the expansion of the items following in results in an empty list, no commands are executed, and the return status is 0.
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETICEVALUATION. The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression 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 list that is executed, or false if any of the expressions is invalid.
select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed. The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.
case word in [ [(] pattern [ | pattern ]
A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname expansion (see Pathname Expansion below). When a match is found, the corresponding list is executed. After the first match, no subsequent matches are attempted. The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list.
the if List ; Then List; [ Elif List ; Then List ; ] ... [ ELSE List ; ] - Fi
The if list is executed. If its exit status is zero, the then list is executed. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. Otherwise, the else list is executed, if present. The exit status is the exit status of the last command executed, or zero if no condition tested true.
while list; do list; done
until list; do list; done
The while command continuously executes the do list as long as the last command in list returns an exit status of zero. The until command is identical to the while command, except that the test is negated; the do list is executed as long as the last command in list returns a non-zero exit status. The exit status of the while and until commands is the exit status of the last do list command executed, or zero if none was executed.
[ function ] name () { list; }
This defines a function named name. The body of the function is the list of commands between { and }. This list is executed whenever name is specified as the name of a simple command. The exit status of a function is the exit status of the last command executed in the body. (See FUNCTIONS below.) 

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11076467.html
Recommended