Shell symbol expansion

Source: Internet
Author: User
Tags arithmetic echo command integer division

Character expansion
*
This "*" character means matching any character in the file name
The shell expands "*" into something else, before the echo command is executed.

~ Home Directory
Arithmetic expression expansion
An arithmetic expression expands using this format:
$ ((expression))
echo $ ((+))
An arithmetic expression supports only integers (all numbers, without decimal points), but can perform many different operations. Here is
Some of the operators it supports:
Operator description
+ Plus
-Minus
* Multiply
/except (but remember, because the expansion supports integer division only, the result is an integer
Number. )
%uh the remainder, just simply means, "remainders"
* * Take power
In arithmetic expressions, hollow lattices are not important, and expressions can be nested. For example, 5 squared multiplied by 3:
[[email protected] ~]$ echo $ (($ ((5**2)) * 3))
75
A pair of parentheses can be used to enclose multiple sub-expressions. With this technique, we can rewrite the above example to
Use an expansion instead of two to get the same result:
[[email protected] ~]$ echo $ ((((5**2) * 3))

Curly braces unfold
As with Mathematics (A+B) (c+d) = AC+AD+BC+BD


It allows you to create multiple text strings from a pattern that contains curly braces. This is an example:

[Email protected]]# Echo ABC-{A,B,C}-CBA
ABC-A-CBA ABC-B-CBA ABC-C-CBA

The curly brace expansion pattern may contain an initial part called a header, and an end part is called a postscript. Curly Brace Expression
itself may contain a comma-separated list of strings, or a series of integers, or a single string.
This example uses a series of integers:
[[email protected] ~]$ Echo number_{1..5}
number_1 number_2 number_3 number_4 number_5

A series of letters in reverse order:
[email protected] ~]$ echo {Z.. A
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

Curly brace expansion can be nested:

[Email protected]]# Echo a{a{1,2},b{3,4}}b
AA1B aa2b ab3b ab4b

The most common application is to create a series of files or directory listings.
For example, if we are photographers, there are plenty of photos. We want to organize these photos in chronological order. First, we want to create a
Series of directories named "year-month" in the number. In this way, the directory names are sorted chronologically. We can key
into the entire directory list, but the workload is too large and error-prone. Instead, we can do this:
[[email protected]~]$ mkdir Pics
[[email protected]~]$ CD Pics
[[email protected]pics]$ mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12}
[[email protected]pics]$ ls
2007-01 2007-07 2008-01 2008-07 2009-01 2009-07
2007-02 2007-08 2008-02 2008-08 2009-02 2009-08
2007-03 2007-09 2008-03 2008-09 2009-03 2009-09
2007-04 2007-10 2008-04 2008-10 2009-04 2009-10
2007-05 2007-11 2008-05 2008-11 2009-05 2009-11
2007-06 2007-12 2008-06 2008-12 2009-06 2009-12

command SubstitutionCommand substitution allows us to use the output of a command as an expansion mode: Echo $ (LS)Double quotesThe first type of reference, double quotation marks. If you put the text in double quotes, the shell uses special characters, except for the $,\ (backslash), and ' (inverted quotation marks), to lose their special meaning and be treated as plain characters. This means that word splitting, path name expansion, wavy line expansion, and braces expansion are all forbidden, however parameters are expanded, arithmetic is expanded, and command substitution is still performed. Using double quotes, we can handle file names that contain spaces.single quotation marksIf you need to suppress all expansion, we use single quotes. Output comparison Results [[email protected]~]$ echo Text ~/*.txt {A, B} $(Echo foo)$((+)) $USER
Text/home/me/ls-output.txt a b foo 4 me
89
[[email protected]~]$ echo "Text ~/*.txt {A, B} $(Echo foo)$((+)) $USER "
Text ~/*.txt {b} foo 4 me
[[email protected]~]$ echo ' text ~/*.txt {A, B} $ (echo foo) $ (()) $USER '
Text ~/*.txt {A, B} $ (echo foo) $ (()) $USERAnti-Quote' ls ' Escape character sometimes we just want to refer to a single character. We can precede the character with a backslash, which is called the escape character in this context. Escape characters are often used in double quotes to selectively block expansion. Note In single quotes, the backslash loses its special meaning, and it is treated as a normal character.

Shell symbol expansion

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.