Skip to content

Use double bracket for arithmetic operation in bash #2

Description

@mhdzumair

Description:
Sir we can use double bracket for arithmetic operation in bash is more flexible way because it doesn't matter if there is a bracket or not. So I'm kindly suggesting to replace / include this double bracket guide.

Suggested Labels:
Improvement

Suggested Assignees:
Affected Product Version:
OS, DB, other environment details and versions:
Steps to reproduce:
Related Issues:

Current Code:

#!/bin/sh

A=70
B=90

val=`expr $A + $B` #Be Mindful about the spaces in between the "expr" , $A , + , $B  characters, 
#                  which  is a must. Otherwise script will throw a syntax error 
echo "A + B : $val"

val=`expr $A - $B`
echo "A - B : $val"

val=`expr $A \* $B`
echo "a * b : $val"

val=`expr $B / $A`
echo "B / A : $val"

val=`expr $B % $A`
echo "B % A : $val

Suggesting Code

#!/usr/bin/bash

A=70
B=90

((val = $A + $B))
echo "A + B : $val"

((val=$A-$B))
echo "A - B : $val"

((val= $A * $B))
echo "a * b : $val"

((val = $B / $A))
echo "B / A : $val"

((val= $B % $A))
echo "B % A : $val"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions