(go) shell variables and extensions

Source: Internet
Author: User
Tags arithmetic

1. Shell variables

The shell variable assignment statement is "Name=[value", there can be no space on either side of the equal sign, you could append the contents "Name+=value" to the shell variable, cancel the shell variable's setting using "unset name", as shown below.

$Var=1$Echo $var 1$ var= 123$ echo  $var 123$ var+=100$ echo  $var 123100$ var=a$ echo  $vara $ var=abc$ echo  $VARABC $ var+=xxx$ echo  $VARABCXXX $ unset var$ echo  $var             
2. Shell Extension

The command line is split into symbols to be expanded later, in a number of ways, and in a certain order: curly brace extension, tilde extension, parameters, variables and arithmetic extensions, and command substitution (left to right), Word splitting, and file name extension, if supported by the system, there is another extension, process substitution, which is associated with parameters, Variables and arithmetic extensions and command substitutions are performed at the same time. Only the curly brace extension, Word extension and file name extension can change the number of words in the extension, the other extension is a single word to expand into a single word, the only exception is the extension of the "[email protected]" sum "${array[@]}" , all extensions are completed before reference removal. These shell extensions are described below individually.

3, curly brace extension

The curly brace extension is a mechanism that can generate arbitrary strings, in the basic form "PREFIX{VAR,VAR2,VAR3 ...} Suffix "or" prefix{x. y[. Increment]}suffix ". The curly brace extension prefix prefix, suffix suffix is optional, the contents of the curly braces are comma-delimited strings or a sequence expression, extending from left to right. For strings, they are separated by commas, and if there is only one string, the effect of having no commas after the string is different. For a sequence expression, x and Y are an integer or a single character, the type must be the same, the subsequent increment increment is an optional integer value, the default is 1 or-1, based on the size of x and Y, and when x and y are integers, a 0 is added to the front of the integer to qualify the width of the integer. A high-level shortfall is 0-padded and eventually expands to include a series of values from a small to a larger value, including x and Y.

$ foo() { echo a{foo, bar}z; }$ fooafooz abarz$ foo() { echo a{01..10..2}z; }$ fooa01z a03z a05z a07z a09z

A well-formed curly brace extension must contain no referenced opening and closing braces, and at least one comma or sequence expression that is not referenced. Curly braces are extended before all other extensions, and in order to avoid conflicts with parameter extensions, the curly brace extension does not recognize "${" in the string, and in order to prevent it from being considered part of the curly brace extension, curly braces and commas can be escaped with backslashes.

4, the Wave number expansion

If a word begins with an unreferenced tilde "~", all subsequent characters are treated as tilde prefixes until the first unreferenced slash (if any) is not referenced. If the character in the tilde prefix is not referenced, then all characters following the tilde are treated as a possible login user name, and if the login is an empty string, the tilde is replaced with the value of the shell special variable home, if no home is set, Replace it with the home directory of the user who executed the command, or replace it with the one specified in the login name.

~    扩展为"$HOME"~/foo    扩展为"$HOME/foo"~username/foo    扩展为用户username的主目录中的子目录foo

In the tilde prefix, you can have a plus, minus sign.

~+    扩展为"$PWD"~-    扩展为"$OLDPWD"

In the tilde prefix, you can also use integers for the directory stack (corresponding built-in commands for pushd, POPD, dirs) extensions.

~N    命令"dirs +N"显示的字符串~+N    命令"dirs +N"显示的字符串~-N 命令"dirs -N"显示的字符串
5. Parameter (variable) extension

Parameter extensions are guided with the dollar sign "$", and the parameters are typically placed in a pair of unreferenced curly braces, in the following basic format:

${parameter}

There are several cases where the colon ":" is used:

${Parameter-word} ifparameter is not set or empty and is replaced withWord, or replace with theThe value of the parameter. ${Parameter+word} ifparameter is not set or empty, no substitution is made;Word. ${Parameter=word} ifParameter not set or empty, putword assigned to parameter. Eventually replaced with the value parameter. ${parameter: Word} if parameter is not set or empty, word output to stderr, Otherwise, replace with the value of parameter. ${parameter: Offset} expands to parameter a substring starting from offset. ${parameter: Offset:length} extended to parameter from offset the length of the start does not exceed length.  

Use an exclamation mark "!" Several cases (indirect extension):

${!prefix*}    扩展为变量名中含有prefix的一些变量。${[email protected]}    扩展为变量名中含有prefix的一些变量。${!name[*]}    如果name为数组,扩展为name的索引;否则结果为0。如果name未定义,结果为空。${!name[@]} 如果name为数组,扩展为name的索引;否则结果为0。如果name未定义,结果为空。

There are several situations where the pound sign "#" is used:

${#parameter}    结果为parameter所包含的字符数。${parameter#word}    word与parameter从最左边开始进行模式匹配,结果为从parameter最左边删除匹配到的最短字符串后剩下的内容。${parameter##word} word与parameter从最左边开始进行模式匹配,结果为从parameter最左边删除匹配到的最长字符串后剩下的内容。

There are several situations in which percent "%" is used (contrary to "#"):

${parameter%word}    word与parameter从最右边开始进行模式匹配,结果为从parameter最右边删除匹配到的最短字符串后剩下的内容。${parameter%%word}     word与parameter从最右边开始进行模式匹配,结果为从parameter最右边删除匹配到的最长字符串后剩下的内容。

String substitution:

${parameter/pattern/string}    pattern为一种模式,把parameter中与之匹配的最长字符串用string替换。若pattern以#开头,只匹配parameter的开头;若pattern以%开头,只匹配parameter的结尾;若pattern以/开头,会替换所有匹配到的内容,否则只替换第一个匹配到的内容;若string为空,可省略pattern后面的/,表示删除匹配到的内容。

Character-case conversions (the pattern is omitted to indicate that each character can be matched):

${parameter^pattern}    把parameter中与pattern匹配的第一个字符转为大写字母。${parameter^^pattern}    把parameter中与pattern匹配的所有字符转为大写字母。${parameter,pattern}    把parameter中与pattern匹配的第一个字符转为小写字母。${parameter,,pattern}    把parameter中与pattern匹配的所有字符转为小写字母。

6. Arithmetic expansion

The arithmetic extension can complete a true mathematical operation in the form of:

$((expression))

For example:

$ foo=1$ var=$((foo+=10))$ echo $var11

7. Command replacement

The command replaces the standard output of the command execution with the command itself, in the form:

$(command)`command`

For example:

$ uname -px86_64$ foo=$(uname -p)$ echo $foox86_64

8. Process substitution

If the system supports named pipe "FIFO" or is able to name open files in "/dev/fd" mode, then the process substitution is supported, in the format:

<(command)>(command)

There is no space between the angle brackets in the process substitution and the left parenthesis. When executing a command, its input and output are associated with a named pipe FIFO or a file in the/DEV/FD directory, as if the command's input and output are tied to the input and output streams of another process.
For example:

$ echo"Hello" > test.sh$ echo"World" >> test.sh$ cat Test.shhelloworld$ grep Hello < (cat test.sh) Hello$ echo AA bb cc dd > (awk  ' {print $} ') AA bb cc dd/dev/fd/63 $ echo AA bb cc DD &G T /dev/fd/63 > (awk  ' {print $} ') $ aa< Span class= "Hljs-char" >$ echo AA bb cc dd >/dev/fd/63 > (awk  {print $} ') $ bb$ echo AA bb cc dd >/dev/fd/63 > (awk  ' {print $} ') $ cc$ echo AA bb CC DD >/dev/fd/63 > (awk  ' {print $4} ') $ dd              
9. Word splitting

The word split occurs in the shell extension, the associated system variable is IFS, which is the internal Field Separator, and the default is <space><tab><newline> that the delimiters appear at the beginning or end of the shell extension results are ignored, and other places are separated as delimiters.

10. File name extension

After the word is split, bash searches for "*", "?", "[" in each word, if one is found, the pattern is matched, the built-in command shopt is related to pattern matching, and several special symbols in pattern matching are described below.

*    匹配任何字符串,包括空字符串。?    匹配任意单个字符。[...]    匹配方括号中的任一字符。可以是一个范围表达式,由连字符连接一对字符,这个范围受当前语言环境的影响。如果方括号后面的第一个字符是“!”或“^”,则匹配任一没有出现在方括号中的字符。如果要匹配字符“-”,可以把它放在方括号中的第一个或最后一个位置,如果要匹配字符“]”,可以把它放在方括号中的第一个位置。[[:class:]]    通过class指定字符类别,class可以是POSIX标准中的下列关键字:alnum、alpha、ascii、blank、cntrl、digit、graph、lower、print、punct、space、upper、word、xdigit,其中word表示大小写字母、数字和下划线。[[=c=]]    匹配所有的字符c。[[.symbol.]]    匹配所有的符号symbol。?(pattern-list)    匹配pattern-list零次或一次。*(pattern-list)    匹配pattern-list零次或多次。+(pattern-list)    匹配pattern-list一次或多次。@(pattern-list)    匹配pattern-list中的某个模式。!(pattern-list)    与pattern-list中的所有模式都不匹配的其它情形。

11. Reference Delete

After the shell extension mentioned above, for all characters that are not referenced, including the backslash "\", the single quote "'" and the double quotation mark "", if not generated by the shell extension, it will be deleted, resulting in real results.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. 52548525

(go) shell variables and extensions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.