[Shell] Bash variables: numeric operations and operators

Source: Internet
Author: User
Tags logical operators

-------------------------------------------------------------------------------------------------

The types of variables in the shell are all strings by default, so:

#!/bin/Bashaa=1bb=2cc= $aa +$bbEcho $cc        1+2

Number of ways to calculate:

Declare declares the type of the variable:

    declare [+/-] [option] Variable name

Options:

-: Set type attribute to variable

+: Cancels the Type property of a variable

-I: Declaring a variable as an integer (int)

-x: Declaring a variable as an environment variable

-P: Displays the declared type of the specified variable

Aa=1bb=2cc= $aa +-P AA    # DECLARE--aa="1" -P AA # Declare-x Aa= "1", declare has a-CC =       $aa +$BB  # declare cc as numeric   echo $cc         3

Method 1 (DECLARE):

  Declare-i cc= $aa + $bb

Method 2 (expr or let numeric arithmetic tool):

  dd=$ (expr $aa + $bb) # dd value is AA and BB and the + number must have a space on both sides to be valid

Method 3:

  $ (expression) or $[expression] # If you use a single parenthesis, it means that the system command is included

Operators are: Assignment operators, numeric operators, logical operators.

echo $ ((aa=1)) # 1

echo $ ((6%4)) # 6

echo $ ((1 && 0)) # 0

Link:http://www.cnblogs.com/farwish/p/4772110.html

@ Black eyed poet <www.farwish.com>

[Shell] Bash variables: numeric operations and operators

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.