Use of shell Learning () and {}

Source: Internet
Author: User

Use of shell Learning () and {}
In shell command line, the average person may not care much about the differences between the two pairs of symbols () and,
Although both of them can group multiple commands, the technical details are quite different:
() Place the command group in sub-shell for execution, also known as nested sub-shell.

{} Is completed in the same shell, also known as non-named command group.

In bash shell, both $ () and ''(reverse quotation marks) are used for command replacement (command substitution)
.

For the sake of completeness, I will use some examples to illustrate some special functions of $:
Suppose we define a variable:
File =/dir1/dir2/dir3/my.file.txt
We can replace $ {} to get different values:
$ {File # */}: remove the first/and its left string: dir1/dir2/dir3/my.file.txt
$ {File ### */}: remove the last/and its left string: my.file.txt
$ {File # *.}: remove the first. And its left string: file.txt
$ {File ### *.}: remove the last. And its left string: txt
$ {File %/*}: remove the last string/and its right string:/dir1/dir2/dir3
$ {File %/*}: remove the first/and its right string: (null)
$ {File %. *}: remove the last. And its right string:/dir1/dir2/dir3/my. file
$ {File %. *}: remove the first. And its right string:/dir1/dir2/dir3/my
The memory is as follows:
# Remove the left side (on the disk # on the left side of $)
% Is to remove the right side (% on the disk)
A single symbol is the minimum match; two symbols are the maximum match.

You can use $ {} to assign values to different variable states (not set, null, or non-null ):
Optional bytes file-my.file.txt}: If $ file is not set, use my.file.txt as the return value. (Null and non-null)
)
$ {File: -my.file.txt}: If $ file is not set or is null, use my.file.txt as the return value. (Not empty
Value is not processed)
Too many filetings my.file.txt}: No worries
$ File is used
My.file.txt is used as the return value.
$ {File: Invalid my.file.txt}: If $ file is not null, use my.file.txt as the return value. (Not set or empty values)
Invalid variable file=my.file.txt}: If $ file is not set, use my.file.txt as the return value and assign a value to $ file.
Is my.file.txt. (Null and non-null values are not processed)
$ {File: Your my.file.txt}: If $ file is not set or is null, use my.file.txt as the return value.
$ File is assigned to my.file.txt. (Do not process non-null values)
$ {File? My.file.txt}: If $ file is not set, output my.file.txt to STDERR. (Null and non-empty
Value is not processed)
$ {File :? My.file.txt}: If $ file is not set or is null, output my.file.txt to STDERR. (Non
NULL values are not processed)

The length of the variable value can be calculated from $ {# var:
$ {# File} can get 27 because/dir1/dir2/dir3/my.file.txt is exactly 27 bytes...

Generally, variables like A = "a B c def" only replace $ A with A single string,
But to A = (a B c def), it is to define $ A as the number of groups...
The following method can be used to replace the number of bash groups:
$ {A [@]} or $ {A [*]} can obtain a B c def (number of all groups)
$ {A [0]} returns a (number of the first group), and $ {A [1]} returns the number of the second group...

Well, let's introduce the purpose of $ (): it is used for integer calculation.

The variable name in $ () can be replaced by the $ symbol before it, or not, such:

$ A = 5; B = 7; c = 2
$ Echo $ (a + B * c ))
19

In addition, $ () can also be used for different carry (such as binary, octal, and hexadecimal) operations, but the output results are all
Decimal:
Echo $(16 # 2a) returns 42 (hexadecimal to decimal)

In fact, simply using () can also redefine the variable value, or as testing:
A = 5; (a ++) You can redefine $ a as 6
A = 5; (a --) is a = 4
A = 5; B = 7; (a <B) returns 0 (true.
Common test symbols for () are as follows:
<: Less
>: Greater

<=: Less than or equal
>=: Greater than or equal
=: Equal
! =: Not equal

Refer to shell 13th question

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.