$ () And $ {} and $ () and (())

Source: Internet
Author: User
$ () And $ {} and $ () and () $ () and $ {} usage: in bashshell, $ () and ''are used for command replacement (commandsubstitution. $ () Is not applicable to any shell. if you use bash2 ,... $ () and $ {} and $ () and () $ () and $ {} usage: in bash shell, $ () both 'and' are used for command replacement (command substitution. $ () Is not applicable to any shell. if you use bash2, it will certainly be okay... let's see $... it is actually used to replace variables. In general, $ var is not the same as $ {var. However, $ {} is used to precisely define the range of variable names. Here I will use some examples to illustrate some special functions of $ {}: Suppose we have defined a variable: file =/dir1/dir2/dir3/my.file.txt. we can replace $ {} with 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 one. and the string on the left: file.txt $ {file ##*.}: remove the last one. and the left string: txt $ {file %/*}: remove the last string/and the right string:/dir1/dir2/dir3 $ {file % /*}: remove the first/and its right string: (null) $ {file %. *}: remove the last one. and its right string:/dir1/dir2/dir3/my. file $ {file %. *}: remove First. and the string on the right:/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 in the right side of $). is the single symbol the minimum match? The two symbols are the maximum matches. $ {File: 0: 5}: extract the leftmost 5 bytes:/dir1 $ {file: 5: 5}: extract 5 consecutive bytes to the right of 5th bytes: /dir2 we can also replace the string in the variable value: $ {file/dir/path}: replace the first dir with path: /path1/dir2/dir3/my.file.txt $ {file/dir/path}: Replace all dir with path: /path1/path2/path3/my.file.txt using $ {} can also assign values for different variable states (not set, Null, non-null): $ {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 to a null value, use my.file.txt as the return value. (When non-empty values are not processed, when too many file1_my.file.txt}: if $ file is set to empty values or non-empty values, my.file.txt is used as the return value. (Not processed when not set) $ {file: your my.file.txt}: if $ file is not null, use my.file.txt as the return value. (When no value is set and no value is set, do not process invalid variable file=my.file.txt}: if $ file is not set, use my.file.txt as the return value, and assign $ file to my.file.txt. (Null and non-null values are not processed) $ {file: empty my.file.txt}: if the specified filepath is not set to a null value, use my.file.txt as the return value, and assign $ file 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-null values are not processed) $ {file :? My.file.txt}: if $ file is not set or is null, output my.file.txt to STDERR. (When the value is not null, it is not processed.) The above understanding is that you must clearly distinguish the three assignment states unset, null, and non-null. generally, null is related to null. null is not affected if: is not included, and even null is affected if: is included. in addition, the length of the variable value can be calculated by $ {# var}: $ {# file}. The length can be 27, because/dir1/dir2/dir3/my.file.txt is exactly 27 bytes... $ () is used for integer calculation. In bash, the integer operators of $ () include: +-*/: "add, subtract, multiply, and divide ". %: Remainder operation & | ^!: "AND, OR, XOR, NOT" operations. Example: wangnc> a = 5; B = 7; c = 2; wangnc> echo $ a + $ b5 + 7 wangnc> echo $ (a + B * c )) 19 wangnc> echo $ (a * B)/c) 17 wangnc> echo $ ($ a + $ B * $ c )) 19 wangnc> the variable name in $ () can be replaced by the $ symbol before it, or not, such: $ ($ a + $ B * $ c) can also get 19 results. In addition, $ (()) it can also be used for different carry (such as binary, octal, and hexadecimal) operations, but the output results are all in decimal format: echo $(16 # 2a )) the result is 42 (16-carried to decimal) (). In fact, simply using () can also be used to redefine the variable value, or testing: a = 5; (a ++) you can define $ a as 6a = 5; (a --) as a = 4a = 5; B = 7; (a <B) The return value of 0 (true. Common test symbols used for () are as follows: <: less than>: greater than <=: less than or equal to >=: greater than or equal to =: equal! =: Not equal
 
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.