Eight extensions of Shell

Source: Internet
Author: User

Recently, in the process of sorting out bash knowledge, I had the honor to read the man bash document. For a while, I was just like a hacker, and many places I didn't understand were suddenly open-minded. Now I want to share one of them, at the same time, I also made an advertisement for man Bash. When you are helpless in the face of bash problems, don't forget to be accompanied by man bash.

 

Bash supports the following extensions:Brace Expansion,Tilde Expansion,Parameter and Variable Expansion,Command substitution,Arithmetic Expansion,Word splitting, AndPathname Expansion(For the sake of accuracy, the term in the original article is directly applied here ). Another example is process substitution.

 

Their priority is: Brace expansion, Tilde expansion, parameter, variable expansion, arithmetic expansion, command substitution, word splitting, pathname Expansion

Brace Expansion

Brace is worth "{}". This extension is used to generate strings. The following two examples are as follows:

 

If the brackets are in the middle of "..", consecutive characters are generated.

 

Tilde Expansion

Tilde is worth "~", We all know that ~ It refers to home, and there are two other, they are "~ + "And" ~ -", Representing PWD and oldpwd respectively. You can use these two methods to conveniently compile the script. For example:

 

 

Parameter Expansion

This is the most important and commonly used extension in bash, but there are not many people familiar with it. Here we will reveal another unknown side of it:

The standard format is $ {parameter}, abbreviated as $ parameter. Using $ {parameter} can avoid many unnecessary troubles.

$ {Parameter:-Word}, IfParameterIf it is null or no parameter is defined, the default value is used,

$ {Parameter: =Word}, IfParameterIf it is null or no parameter is defined, the default value is assigned.

The difference between the two is that the former does notParameterAssign values. For example:

 

$ {Parameter:?Word}, IfParameterIf it is null or no parameter is defined, the error message is printed.

$ {Parameter: +Word}, IfParameterIf the value is not blank, replace it with word.

 

$ {Parameter:Offset}

$ {Parameter:Offset:Length}

The above two are used to intercept a specified length string. The truncated subscript offset starts from 0 and the length is specified by length. If length is not specified, the end of the truncate will be:

 

$ {!Prefix*}

$ {!Prefix@}

Both of them share the same meaning and are extendedPrefixVariable names starting with ifs are used to separate them. For example:

 

$ {! Name [@]}

$ {! Name [*]}

These two are used to query the underlying objects of an array. The output is the subscript of the name array. Note: This is different from $ {name [@]} and $ {name [*]}. The two are the content of each array element printed, here we are talking about these two are printed array subscripts. For example:

 

In addition, if you use the associative array, it will automatically replace the element whose subscript is 0. For example:

 

$ {#Parameter}, Print the parameter length,

 

$ {Parameter # Word}: Use pathname expansion to extend the word as a pattern, scan the parameter from the beginning, and output the shortest matching string meeting the patter.

$ {Parameter # Word}: Use pathname expansion to extend word as a pattern, scan parameter from the beginning, and output the longest matching string meeting the patter.

$ {Parameter % word}: Use pathname expansion to extend the word as a pattern, scan the parameter from the end, and output the shortest matching string that satisfies the patter.

$ {Parameter % word}, use pathname expansion to extend the word as a pattern, scan the parameter from the end, and output the longest matching string meeting the patter.

$ {Parameter/pattern/string}: replace pattern in parameter with string. From the first scan, pattern uses pathname expansion; Replace the first match

$ {Parameter // pattern/string}. Use string to replace pattern in parameter. From scratch scan, pattern uses pathname expansion; replace all matches

 

In addition, if parameter is an array, each element of the array adopts the same policy, for example:

 

Command substitution

Common functions are $ (command) and 'command'. Generally, everyone on Earth knows how to use them. But what if we want to nest it? After reading the following example, you will understand:

 

By the way, we use backslash transfer to implement nesting.

Arithmetic Expansion

Format: $ (expression), for example:

 

Process substitution

This extension was not mentioned at the beginning. This extension uses FIFO, so it can only be used in systems that support FIFO. The format is <(command list) or> (command list ). When this extension is used, it will generate a temporary file in/dev/FD to receive the output of the command list, and then send the content to the current command. This command is useful when you need to temporarily Save the output of several commands and use the output for subsequent processing. This can avoid generating too many temporary files. For example:

 

From the output of the last command, we can see that this replacement puts the intermediate results in two temporary files:/dev/FD/63 and/dev/FD/62. After the command is complete, the two files will disappear.

Word splitting

As the name implies, output is separated by IFS, for example:

 

Pathname Expansion

Is our commonly used ls? Ls *.? . However, this extension behavior is related to multiple settings in set and shopt. The following is an example:

Noglob-D does not allow extension by path name. That is, the wildcard is disabled. (Set-O can be seen, and shopt can be seen later)

Dotglob bash includes a file name starting with a vertex (.) in the file name extension result.

Extglob enables extended pattern matching features (the normal expression metacharacters come from the file name extension of the Korn shell)

If nocaseglob is set, bash matches the file name in case-insensitive mode when file name extension is executed.

If nullglob is set, bash allows the file name pattern that does not match any files to be extended into an empty string, rather than their own

Failglob if no results are matched for pattern, an error is prompted.

The meaning is quite clear. I will not give an example here.

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.