Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Shell Script to Add Two Numbers

    Shell Script to Add Two Numbers

    RahulBy RahulJune 5, 20212 Mins ReadUpdated:July 7, 2021

    Calculating the sum of two integers (Numbers) in a shell script is pretty simple as other programming languages. Bash shell provides a command-line utility called expr to evaluate expressions. The latest version of Bash shell also includes the functionality to evaluate expressions directly with the shell.

    In this tutorial, we will discuss few methods to calculate the sum of the two numbers in a bash script.

    Bash – Adding Two Numbers

    The expr is the command-line utility used for evaluating mathematical expressions. Bash shell also supports to evaluate the mathematical expressions directly.

    Use the following syntax to calculate the sum of two integers in a shell script:

    • Using expr command with quotes
      sum=`expr $num1 + $num2`
      
    • Use expr command inclosed with brackets and start with dollar symbol.
      sum=$(expr $num1 + $num2)
      
    • This is my preferred way to directly with the shell.
      sum=$(($num1 + $num2))
      

    In the next few examples, we will discuss calculating the sum of numbers directly with a shell. You can also choose expr command to give the syntax above.

    Calculate Sum in Shell

    Bash shell also evaluates the mathematical expressions directly. You just need to write the expressions enclosed in double brackets with a dollar like $((...)).

    Write an example shell script to initialize two numeric variables. Then perform an addition operation on both values and store results in the third variable.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!/bin/bash
    # Calculate the sum of two integers with pre initialize values
    # in a shell script
     
    a=10
    b=20
     
    sum=$(( $a + $b ))
     
    echo "Sum is: $sum"

    Output:

    Sum is: 30
    

    Calculate Sum with Command Line Arguments

    In this second example, the shell script reads two numbers as command line parameters and performs the addition operation.

    Shell
    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash
    # Calculate the sum via command-line arguments
    # $1 and $2 refers to the first and second argument passed as command-line arguments
     
    sum=$(( $1 + $2 ))
     
    echo "Sum is: $sum"  

    Let’s execute this script is a shell

    ./sum.sh 12 14        # Executing script 
    
    Sum is: 26
    

    Calculate Sum with Run Time Input

    Here is another example of a shell script, which takes input from the user at run time. Then calculate the sum of given numbers and store to a variable and show the results.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    # Take input from user and calculate sum.
    read -p "Enter first number: " num1
    read -p "Enter second number: " num2
    sum=$(( $num1 + $num2 ))
    echo "Sum is: $sum"  

    Output:

    Enter first number: 12
    Enter second number: 15
    Sum is: 27
    

    Conclusion

    In this tutorial, you have learned few methods to add two numbers in bash shell.

    bash expr
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Nagios Client (NRPE) on Ubuntu 20.04
    Next Article Initial Server Setup with Debian 10/9/8

    Related Posts

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:May 9, 20223 Mins Read

    How To Install Node.js on Ubuntu 22.04

    Updated:April 16, 20223 Mins Read

    How To Install NVM on Ubuntu 22.04

    Updated:April 16, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    4 Comments

    1. C G Balu on December 11, 2021 10:28 am

      How to add two float value using shell script

      Reply
    2. Krishna Kumar on October 8, 2021 2:43 pm

      Bro use this header file at the top of your code

      #!/bin/bash

      Reply
    3. dixit on September 29, 2021 6:27 am

      code :
      echo -n “enter the num1 ==> ”
      read num1
      echo -n “enter the num2 ==> ”
      read num2
      sum =$(( $num1 + $num2 ))
      echo “the sum of $num1 and $num2 is ==> $sum”

      i have written this code but it shows me some errors so can you help me ?

      error :
      enter the num1 ==> 10
      enter the num2 ==> 20
      sum: ‘=30’: No such file or directory
      the sum of 10 and 20 is ==>

      Reply
      • Rahul on September 29, 2021 7:31 am

        Hi Dixit,
        Remove space after “sum variable”

        sum=$(( $num1 + $num2 ))
        

        The full script should be

        echo -n "enter the num1 ==> "
        read num1
        echo -n "enter the num2 ==> "
        read num2
        sum=$(( $num1 + $num2 ))
        echo "the sum of $num1 and $num2 is ==> $sum"
        
        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.