Variable operations in Bash

Source: Internet
Author: User
Article title: Operations on variables in Bash. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   1. conditional variable replacement:
Bash Shell can replace the conditions of a variable. it can be replaced only when a condition occurs, and the replacement conditions are placed in.
(1) $ {value:-word}
If the variable is undefined or the value is null, the return value is the content of the word; otherwise, the value of the variable is returned.
(2) $ {value: = word}
Similar to the former, if the variable is undefined or the value is null, the word is assigned to the value when the value of the word is returned.
(3) $ {value :? Message}
If the variable is assigned a value, it will be replaced normally. Otherwise, the message will be sent to the standard error output (if this replacement occurs in the Shell program, the program will terminate the operation)
(4) $ {value: + word}
If a variable is assigned a value, the value is replaced by word. otherwise, no replacement is performed.
(5) $ {value: offset}
$ {Value: offset: length}
Extract substrings from Variables. here, offset and length can be arithmetic expressions.
(6) $ {# value}
Variable characters
(7) $ {value # pattern}
$ {Value ## pattern}
Remove the part in the value that matches pattern. the condition is that the start of value matches pattern. The difference between # and # is that one is the shortest matching mode and the other is the longest matching mode.
(8) $ {value % pattern}
$ {Value % pattern}
Similar to (7), except that the value is matched from the end of pattern. The difference between % and % is the same as that between # and #.
(9) $ {value/pattern/string}
$ {Value // pattern/string}
Replace the variable content, and replace the matching part with the string content. The difference between/And // is the same
  
Note: Except (2) in the replacement of the above conditional variables, other values of the variables themselves are not affected.
  
   2. arithmetic operations on variables
In Bash Shell, only two integers can be computed, and the result is still an integer. to perform arithmetic operations, run the let command. syntax:
Let expr
Expr is an expression that contains items and operators. an item can be a variable or an integer constant. when an integer constant is used, it is a decimal integer by default, you can use radio # number to specify other integers. In this example, radio defines the number of integers in hexadecimal notation, and number indicates the value of this integer. if radio> 10, numbers can be from 0-9 and A-Z.
Operators supported in expressions and their meanings are:
+,-, *,/, % Plus, subtraction, multiplication, division, modulo
>>,<<,&, ^, | Shifts left, right, bitwise AND, bitwise OR, bitwise OR
? : Ternary operator. consistent with the definition in C language
~ CAPTCHA
!,> =, <=,>, <, = ,! =, &, |
=, + =,-=, * =,/=, % =, <=, >>=, & =, ^ =, | =
Brackets can also be used in expression expressions. brackets or calculation priority definitions are the same as in general computer languages.
The let command has a return value. when the calculation result (if multiple expressions exist, the last one prevails) is 0, the return value is 1. Otherwise, the return value is 0.
When the expression contains special shell characters (such as |), you must use a quote ('or ") to reference it.
When using let, you should also pay attention to the fact that for a child such as let x + y, shell will discard the result even though it calculates the value of x + y. If this is not the case, you can use let sum = x + y to save the result of x + y in the sum variable.
In addition, you can use the (and) operator to replace the let command. in this case, you can also omit the reference to the arithmetic expression. if you want to return the value of the expression, the format of $ () is required.

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.