Unix addition script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Im currently trying to make a addition command in unix and have come up with the following code:



#! /bin/bash
#! Add - adds two given numbers together and displays the result

"$num1" = $1
"$num2" = $2

echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"


This however does not work.










share|improve this question



















  • 1




    Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
    – Eric Renouf
    Nov 25 '15 at 19:38










  • When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
    – S.Jones
    Nov 25 '15 at 19:46







  • 1




    num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
    – chepner
    Nov 25 '15 at 21:14















up vote
0
down vote

favorite












Im currently trying to make a addition command in unix and have come up with the following code:



#! /bin/bash
#! Add - adds two given numbers together and displays the result

"$num1" = $1
"$num2" = $2

echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"


This however does not work.










share|improve this question



















  • 1




    Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
    – Eric Renouf
    Nov 25 '15 at 19:38










  • When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
    – S.Jones
    Nov 25 '15 at 19:46







  • 1




    num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
    – chepner
    Nov 25 '15 at 21:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Im currently trying to make a addition command in unix and have come up with the following code:



#! /bin/bash
#! Add - adds two given numbers together and displays the result

"$num1" = $1
"$num2" = $2

echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"


This however does not work.










share|improve this question















Im currently trying to make a addition command in unix and have come up with the following code:



#! /bin/bash
#! Add - adds two given numbers together and displays the result

"$num1" = $1
"$num2" = $2

echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"


This however does not work.







shell-script command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:48









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Nov 25 '15 at 19:35









S.Jones

483




483







  • 1




    Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
    – Eric Renouf
    Nov 25 '15 at 19:38










  • When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
    – S.Jones
    Nov 25 '15 at 19:46







  • 1




    num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
    – chepner
    Nov 25 '15 at 21:14













  • 1




    Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
    – Eric Renouf
    Nov 25 '15 at 19:38










  • When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
    – S.Jones
    Nov 25 '15 at 19:46







  • 1




    num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
    – chepner
    Nov 25 '15 at 21:14








1




1




Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38




Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38












When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46





When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46





1




1




num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
– chepner
Nov 25 '15 at 21:14





num1=$1. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
– chepner
Nov 25 '15 at 21:14











2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










((...)) is the way to do arithmetic, not single parens, and you don't need quotes there Try:



sum=$((num1+num2))





share|improve this answer



























    up vote
    0
    down vote













    Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.



    That means that the script could be reduced to



    #!/bin/sh

    printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"


    This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.



    The script would be used as



    $ ./script.sh -23 32
    The sum of -23 and 32 is 9





    share|improve this answer




















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f245498%2funix-addition-script%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      ((...)) is the way to do arithmetic, not single parens, and you don't need quotes there Try:



      sum=$((num1+num2))





      share|improve this answer
























        up vote
        2
        down vote



        accepted










        ((...)) is the way to do arithmetic, not single parens, and you don't need quotes there Try:



        sum=$((num1+num2))





        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          ((...)) is the way to do arithmetic, not single parens, and you don't need quotes there Try:



          sum=$((num1+num2))





          share|improve this answer












          ((...)) is the way to do arithmetic, not single parens, and you don't need quotes there Try:



          sum=$((num1+num2))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '15 at 19:44









          Eric Renouf

          13.2k42949




          13.2k42949






















              up vote
              0
              down vote













              Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.



              That means that the script could be reduced to



              #!/bin/sh

              printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"


              This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.



              The script would be used as



              $ ./script.sh -23 32
              The sum of -23 and 32 is 9





              share|improve this answer
























                up vote
                0
                down vote













                Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.



                That means that the script could be reduced to



                #!/bin/sh

                printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"


                This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.



                The script would be used as



                $ ./script.sh -23 32
                The sum of -23 and 32 is 9





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.



                  That means that the script could be reduced to



                  #!/bin/sh

                  printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"


                  This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.



                  The script would be used as



                  $ ./script.sh -23 32
                  The sum of -23 and 32 is 9





                  share|improve this answer












                  Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.



                  That means that the script could be reduced to



                  #!/bin/sh

                  printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"


                  This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.



                  The script would be used as



                  $ ./script.sh -23 32
                  The sum of -23 and 32 is 9






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 20:54









                  Kusalananda

                  116k15218352




                  116k15218352



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f245498%2funix-addition-script%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay