Tag: Shell
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
Use of shell Learning () and {}