The difference between $ (()) in the shell and $ () and ${}

Source: Internet
Author: User
Tags arrays

Original address: http://blog.zol.com.cn/2322/article_2321763.html

$ () and ' (anti-quote)

In the bash shell, $ () and "(anti-quote) are used for command substitution.
$ echo the last Sunday was $ (date-d "last Sunday" +%y-%m-%d)
Get the date of the previous Sunday

The reason for using $ ()

1. "Easy to mix with" (single quote). Sometimes in some strange glyphs, the two symbols are identical (vertical two points).
2. In a multi-level compound substitution, ' extra hops (\ ') are required, while $ () is more intuitive. For example:
Command1 ' Command2 ' Command3 '
The original intention is to Command2 ' Command3 ' in the first to replace the Command3 to Command2 processing, and then pass the results to Command1 ' Command2 ... ' to deal with.
However, the real results are divided into ' command2 ' and ' two segments ' in the command line.
The correct input should be as follows:
Command1 ' command2 \ ' command3\ '
Change to $ () at a glance:
Command1 $ (Command2 $ (COMMAND3))

$ () Insufficient

"Basically, it can be used in all of the Unix shells, if it's written in shell script, the transplant is relatively high. and $ () not every shell can be used.

${} used for variable substitution

In general, the $var works the same as ${var}. However, using ${} will be more precise in defining the range of variable names, for example:
$ a=b
$ echo $AB
Originally intended to replace the result of a $ A and then a B-letter followed, but on the command line, the real result is only replace the value of the variable name is AB out.
Using ${} is no problem:
$ echo ${a}b
Bb

Some psychic powers of ${}

Define a variable:
File=/dir1/dir2/dir3/my.file.txt
You can use ${} to replace each other to get different values:
${file#*/} take out the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/} take out the last/and left string: my.file.txt
${file#*.} Take out the first one. And the string to the left: file.txt
${file##*.} Take off the last one. And the string to the left: txt
${file%/*} take out the last/And right string:/dir1/dir2/dir3
${file%%/*} take out the first/and the right string: (null value)
${file%.*} take off the last one. And the string to the right:/dir1/dir2/dir3/my.file
${file%%.*} take off the first one. And the string to the right:/dir1/dir2/dir3/my
Methods of Memory:
# Remove the left side (on the keyboard # on the left side)
% Remove right (on keyboard% on right of $)
The single symbol is the minimum match, and the two symbols are the maximum matches.
${file:0:5} extracts the leftmost 5 bytes:/dir1
${file:5:5} extracts the 5th byte to the right of 5 consecutive bytes:/DIR2
You can also replace the string in the value of the variable:
${file/dir/path} replaces the first dir with a path:/path1/dir2/dir3/my.file.txt
${file//dir/path} Replace all dir with Path:/path1/path2/path3/my.file.txt
With ${} You can also assign values to different variable states (not set, NULL, non-null values):
${file-my.file.txt} If $file is not set, use My.file.txt as the return value. (null and non-null values are not processed)
${file:-my.file.txt} If $file is not set or null, use My.file.txt to return the value. (Non-null value is not processed)
${file+my.file.txt} If the $file is set to a null value or a non-null value, the value is returned using My.file.txt. (not processed when not set)
${file:+my.file.txt} If $file is a non-null value, use My.file.txt as the return value. (not processed when not set and null value)
${file=my.file.txt} If $file is not set, use My.file.txt as the return value and assign the $file to My.file.txt. (null and non-null values are not processed)
${file:=my.file.txt} If $file is not set or NULL, My.file.txt is used to return the value, and the $file is assigned a value of My.file.txt. (Non-null value is not processed)
${file?my.file.txt}: If the $file is not set, the My.file.txt output to STDERR. (null and non-null values are not processed)
${file:?my.file.txt}: If $file is not set or null, the my.file.txt output to STDERR. (Non-null value is not processed)
The above understanding is, must distinguish Chu unset with null and non-null these three kinds of assignment state.
In general, NULL is concerned, without: null is not affected, if band: then even null is affected.
${#var} to calculate the length of the variable value:
${#file} can get 27,/dir1/dir2/dir3/my.file.txt just 27 bytes.

Bash Arrays (array) processing method

In general, a= "a b C def" simply replaces the $A with a single string, but instead a= (a b C def), the $A is defined as an array.
The array substitution method for Bash can be found in the following ways:
${a[@]} or ${a[*]} to get A b C def (all arrays)
${a[0]} Get a (first element), ${a[1]} second ...
${#A [@]} or ${#A [*]} Get 4 (number of arrays)
${#A [0]} gets 1 (the length of the first element a), ${#A [3]} gets 3 (the length of the fourth element Def)
A[3]=XYZ to redefine the fourth element as XYZ

Purpose of $ (())

The

is used for integer arithmetic. In bash, the integer operation symbol for $ (()) roughly has these:
+-*/Add, subtract, multiply, divide
% remainder Operation
& | ^! And, or, XOR, not operation
Example:
$ a=5; b=7; c=2
$ echo $ ((a+b*c))

$ echo $ (((a+b)/C))
6
$ echo $ (((a*b %c))
1
The variable name in $ (()) can also be preceded by a $ symbol: $ (($a + $b * $c)) can also get 19 results.
In addition, $ (()) can also be used for different binary (such as binary, octal, hexadecimal) operations, but the output is only decimal. The
Echo $ ((16#2A)) result is 42 (16 decimal)
to give a practical example:
The current umask is 022, the permissions for the new file are:
$ umask 022
$ echo "OBASE=8; $ ((8#666 & (8#777 ^ 8#$ (umask))) "| BC
644
In fact, it is possible to redefine the value of a variable by simply (()), or as a testing:
A=5; ((a++)) to redefine $a to 6
a=5; ((A –)) a=4
a=5; b=7; ((a < b) will get a return value of 0 (true)
The common test Symbols for (()) have the following:
< less than
> greater than
<= less than or equal to
>= greater than or equal to
= = equals
! = is not equal to

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.