Linux Shell
约定(DEFINITIONS)
The following definitions are used throughout the rest of this document.
1 | blank A space or tab. |
Shell 参数
1 | Special Parameters |
单引号、双引号以及没有引号的区别
-
单引号:
可以说是所见即所得:即将单引号内的内容原样输出,或者描述为单引号里面看见的是什么就会输出什么。 -
双引号:
把双引号内的内容输出出来;如果内容中有命令,变量等,会先把变量,命令解析出结果,然后在输出最终内容来。
双引号内命令或变量的写法为命令或变量
或$(命令或变量)。 -
无引号:
把内容输出出来,不会将含有空格的字符串视为一个整体输出;
如果内容中有命令、变量等,会先把变量、命令解析结果,然后在输出最终内容来;
如果字符串中带有空格等特殊字符,则不能完整的输出,需要改加双引号,一般连续的字符串,数字,路径等可以用,不过最好用双引号替代之
条件表达式(CONDITIONAL EXPRESSIONS)
Conditional expressions are used by the [[ compound command and the test and [
builtin commands to test file attributes and perform string and arithmetic com‐
parisons. Expressions are formed from the following unary or binary primaries.
If any file argument to one of the primaries is of the form /dev/fd/n
, then file
descriptor n
is checked. If the file argument to one of the primaries is one of
/dev/stdin
, /dev/stdout
, or /dev/stderr
, file descriptor 0
, 1
, or 2
,
respectively, is checked.
Unless otherwise specified, primaries that operate on files follow symbolic links
and operate on the target of the link, rather than the link itself.
When used with [[
, the <
and >
operators sort lexicographically(字典顺序) using
the current locale. The test command sorts using ASCII ordering.
1 | -a file |
算术求值(ARITHMETIC EVALUATION)
The shell allows arithmetic expressions to be evaluated, under certain circumstances(情景) (see the let and declare builtin commands and Arithmetic Expansion).
Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error.
The operators and their precedence, associativity, and values are the same as in the C language.
运算符及其优先级,关联性和值与C语言中的相同。
The following list of operators is grouped into levels of equal-precedence operators.
The levels are listed in order of decreasing precedence.
1 | id++ id-- |
Shell variables are allowed as operands; parameter expansion is performed before
the expression is evaluated. Within an expression, shell variables may also be
referenced by name without using the parameter expansion syntax. A shell vari‐
able that is null or unset evaluates to 0 when referenced by name without using
the parameter expansion syntax. The value of a variable is evaluated as an
arithmetic expression when it is referenced, or when a variable which has been
given the integer attribute using declare -i is assigned a value. A null value
evaluates to 0. A shell variable need not have its integer attribute turned on
to be used in an expression.
Constants with a leading 0
are interpreted as octal numbers.A leading 0x
or 0X
denotes hexadecimal. Otherwise, numbers take the form [base#]n
,
where the optional base
is a decimal number between 2 and 64
representing the arithmetic base, and n
is a number in that base. If base#
is omitted, then base 10 is used.
1 | e.g.: |
The digits greater than 9 are represented by the lowercase letters, the uppercase
letters, @, and _, in that order. If base is less than or equal to 36, lowercase
and uppercase letters may be used interchangeably to represent numbers between 10
and 35.
64进制: 10个数字(09)+26个小写(az)+26个大写(A~Z)+1个@+1个_。
1 | e.g.: |
Operators are evaluated in order of precedence. Sub-expressions in parentheses
are evaluated first and may override the precedence rules above.
按优先顺序评估运算符。 首先评估括号中的子表达式,并可以覆盖上面的优先级规则。
算术扩展(Arithmetic Expansion)
Arithmetic expansion allows the evaluation of an arithmetic expression and the
substitution of the result. The format for arithmetic expansion is:
``$((expression))``
The expression is treated as if it were within double quotes, but a double quote
inside the parentheses is not treated specially. All tokens in the expression
undergo parameter expansion, string expansion, command substitution, and quote
removal. Arithmetic expansions may be nested.
The evaluation is performed according to the rules listed below under ARITHMETIC
EVALUATION. If expression is invalid, bash prints a message indicating failure
and no substitution occurs.
循环
for name [ [ in [ word ... ] ] ; ] do list ; done
1 | #e.g.: |
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
The arithmetic expression expr1, expr2 expr3.
1 | #e.g.: |
select name [ in word ] ; do list ; done
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
1 | #e.g.: |
if list; then list; [ elif list; then list; ] ... [ else list; ] fi
1 | #e.g.: |
while list-1; do list-2; done
until list-1; do list-2; done
1 | #e.g.: |
数组(Arrays)
Bash provides one-dimensional indexed and associative array variables.
Any variable may be used as an indexed array; the declare builtin will explicitly declare
an array. There is no maximum limit on the size of an array, nor any requirement
that members be indexed or assigned contiguously(连续). Indexed arrays are referenced
using integers (including arithmetic expressions) and are zero-based;
associative arrays are referenced using arbitrary(随意) strings.
An indexed array is created automatically if any variable is assigned to using
the syntax name[subscript]=value
. The subscript is treated as an arithmetic
expression that must evaluate to a number. If subscript evaluates to a number
less than zero, it is used as an offset from one greater than the array’s maximum
index (so a subcript of -1 refers to the last element of the array). To explicitly
declare an indexed array, use declare -a name
(see SHELL BUILTIN COMMANDS
below). declare -a name[subscript]
is also accepted; the subscript is ignored.
Associative arrays are created using declare -A name.
Attributes may be specified for an array variable using the declare and readonly
builtins. Each attribute applies to all members of an array.
Arrays are assigned to using compound assignments of the form name=(value1 ...valuen)
,
where each value is of the form [subscript]=string
. Indexed array
assignments do not require the bracket and subscript. When assigning to indexed
arrays, if the optional brackets and subscript are supplied, that index is
assigned to; otherwise the index of the element assigned is the last index
assigned to by the statement plus one. Indexing starts at zero.
When assigning to an associative array, the subscript is required.
This syntax is also accepted by the declare builtin. Individual array elements
may be assigned to using the name[subscript]=value
syntax introduced above.
Any element of an array may be referenced using ${name[subscript]}
. The braces
are required to avoid conflicts with pathname expansion. If subscript is @
or *
,
the word expands to all members of name. These subscripts differ only when the
word appears within double quotes. If the word is double-quoted, ${name[*]}
expands to a single word with the value of each array member separated by the
first character of the IFS
special variable, and ${name[@]}
expands each element
of name to a separate word. When there are no array members, ${name[@]}
expands
to nothing. If the double-quoted expansion occurs within a word, the expansion
of the first parameter is joined with the beginning part of the original word,and
the expansion of the last parameter is joined with the last part of the original word.
This is analogous to the expansion of the special parameters *
and @
(see Special Parameters ). ${#name[subscript]}
expands to the length of
${name[subscript]}
. If subscript is *
or @
, the expansion is the number of
elements in the array. Referencing an array variable without a subscript is
equivalent to referencing the array with a subscript of 0.
An array variable is considered set if a subscript has been assigned a value.
The null string is a valid value.
The unset builtin is used to destroy arrays. unset name[subscript]
destroys the
array element at index subscript. Care must be taken to avoid unwanted side
effects caused by pathname expansion. unset name, where name is an array, or
unset name[subscript]
, where subscript is *
or @
, removes the entire array.
The declare, local, and readonly builtins each accept a -a
option to specify an
indexed array and a -A
option to specify an associative array. If both options
are supplied, -A
takes precedence(优先). The read builtin accepts a -a
option to
assign a list of words read from the standard input to an array. The set and
declare builtins display array values in a way that allows them to be reused as
assignments.
1 | e.g.: |
参考
- Linux Manual.