[Shell] arithmetic in Shell

Source: Internet
Author: User

Shell script variables are processed as strings rather than numbers by default, which makes it complicated to perform mathematical operations in shell scripts. Perl and python will be a better choice to maintain Script Programming specifications and better arithmetic support. However, you can still perform arithmetic operations in shell. In fact, Unix has added many features over the past few years to support digital processing.

Note: As shown below, some commands that help to process numbers are still not complete in some convenience scenarios, such as spaces around operators.

 

Declare

In shell, you do not need to declare variables in advance, but if you declare an integer variable in advance, the following example shows how the integer variable works:

$ n=6/3$ echo $n6/3$ declare -i n$ n=6/3$ echo $n2

 

If you decide to use an existing program or built-in command to process arithmetic, you do not need to use the declare statement.

 

Expr

Expr is an old UNIX program that can be used for arithmetic value calculation. Expr was widely known during the Bourne shell period because the Bourne shell does not support arithmetic. But in bash and Korn shell, it is generally not used. When using expr, the variable is still processed as a string, and it is not flexible enough to process spaces. It is required to use spaces to separate variables in the expression.

$ z=5$ z=`expr $z+1`   ---- Need spaces around + sign.$ echo $z5+1$ z=`expr $z + 1`$ echo $z6

 

Let

The built-in bash and Korn shell commands for processing arithmetic operations are let. It is not flexible in space processing, and is opposite to expr, it requires that spaces cannot be used between variables.

$ let z=5$ echo $z5$ let z=$z+1$ echo $z6$ let z=$z + 1     # --- Spaces around + sign are bad with let-bash: let: +: syntax error: operand expected (error token is "+")$let z=z+1         # --- look Mom, no $ to read a variable.$echo $z7

 

If you do not want to bother with spaces, use parentheses to enclose the entire statement, so that you can evaluate the values if there are any spaces.

$ ((e=5))$ echo $e5$ (( e = e + 3 ))$ echo $e8$ (( e=e+4 ))  # -- spaces or no spaces, it doesn‘t matter$ echo $e12

 

BC

What if I want to process floating-point numbers or more complex operations? The let command does not support floating-point numbers. Therefore, you must use the BC command, but you must use the entire arithmetic expression as a string for BC processing.

 

If you use the let command to process floating points, the following error example is displayed:

$let r=3.5-bash: let: r=3.5: syntax error in expression (error token is ".5")$(( r = 3.5 ))-bash: ((: r = 3.5 : syntax error in expression (error token is ".5 ")

 

The BC command can handle mathematical operations with any precision. BC has two modes: Interactive Mode and shell script command mode. In interactive mode, use the cntrl-D (EOF) or quit command to exit.

 

Interaction Mode:

$ bcbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty‘.3 + 25obase=2121100<cntrl-d>

 

Script Mode:

$r=3.5$s=`echo "$r + 2.2" | bc`$echo $s5.7$ z = `echo $z + 1 | bc`-bash: z: command not found                # -- spaces around = sign are bad                                (shell thing, not bc)$ z=`echo "$z + 1" | bc`$ echo $z8$ z=`echo "$z+1" | bc`  -- spaces don‘t matter with bc$ echo $z9

 

Numeric Boolean expression

Integer1-EQ integer2: integer1 equals integer2

Integer1-ge integer2: integer1 is greater than or equal to integer2

Integer1-GT integer2: integer1 is greater than integer2

Integer1-Le integer2: integer1 is less than or equal to integer2

Integer1-lt integer2: integer1 less than integer2

Integer1-ne integer2: integer1 is not equal to integer2

 

The following two if statements are equivalent:

if (( x < y )); then    statementsfiif [ $x -lt $y ]; then    statementsfi

 

For floating point arithmetic, BC returns 1 to indicate true, and 0 to indicate false:

if [ $( echo "$t < 3.4" | bc ) -eq 1 ]; then    statementsfi

 

 

Reference 1

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.