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

Source: Internet
Author: User
Tags arithmetic

Tag: equals $? Additional command redefine the txt dir bin integer

$ () and ' (anti-quote)


In the bash shell, $ () and "(anti-quote) are used for command substitution.

# echo ' which who '

# echo $ (which who)

/usr/bin/who

They're getting the same results.


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))


# echo ' ll ' which who '

Total 0which

# echo ' ll \ ' which who\ '

-rwxr-xr-x. 1 root root 49872 Nov 6 2016/usr/bin/who

# echo $ (ll ' which who ')

-rwxr-xr-x. 1 root root 49872 Nov 6 2016/usr/bin/who

# echo $ (ll $ (which who))

-rwxr-xr-x. 1 root root 49872 Nov 6 2016/usr/bin/who


$ () 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


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


${#var} to calculate the length of the variable value:

echo ${#file} output 27

The/dir1/dir2/dir3/my.file.txt is exactly 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


Same use of $ (()) and $[]


Used for integer arithmetic. In bash, the integer operation symbol for $ (()) roughly has these:

+-*/Add, subtract, multiply, divide

% remainder operation

& | ^ ! And, or, XOR, not arithmetic

Example:

# a=5; b=7; c=2

# echo $ ((a+b*c))

# echo $[a+b*c]

19

# echo $ (((a+b)/C))

# echo $[(A+B)/c]

6

# echo $ (((a*b)%c))

# 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.

echo $ ((16#2A)) result is 42 (16 decimal)

To give a practical example:

The current umask is 022, and 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

# echo $ ((a++))

Redefine A to 6

# echo $[a--]

# 5

# echo $ ((a--))

# 4

a=5; b=7; ((a < b)); echo $? Will get a return value of 0 (TRUE)

Common test symbols for (()) have the following:

< less than

> Greater than

<= less than or equal to

>= greater than or equal to

= = equals

! = does not equal


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

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.