Issues with script for adding numbers

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











up vote
1
down vote

favorite












I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.



#!/bin/bash

sum=0

for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number

done

echo $sum






share|improve this question


















  • 1




    Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
    – DopeGhoti
    Mar 23 at 19:13







  • 1




    Why the loop? sum=$(($1 + $2 + $3 + $4)) is enough IMHO.
    – nohillside
    Mar 23 at 19:14










  • I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
    – Î±Ò“sнιη
    Mar 23 at 19:22















up vote
1
down vote

favorite












I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.



#!/bin/bash

sum=0

for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number

done

echo $sum






share|improve this question


















  • 1




    Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
    – DopeGhoti
    Mar 23 at 19:13







  • 1




    Why the loop? sum=$(($1 + $2 + $3 + $4)) is enough IMHO.
    – nohillside
    Mar 23 at 19:14










  • I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
    – Î±Ò“sнιη
    Mar 23 at 19:22













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.



#!/bin/bash

sum=0

for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number

done

echo $sum






share|improve this question














I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.



#!/bin/bash

sum=0

for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number

done

echo $sum








share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 20:18









Kusalananda

102k13201317




102k13201317










asked Mar 23 at 19:09









pvman

61




61







  • 1




    Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
    – DopeGhoti
    Mar 23 at 19:13







  • 1




    Why the loop? sum=$(($1 + $2 + $3 + $4)) is enough IMHO.
    – nohillside
    Mar 23 at 19:14










  • I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
    – Î±Ò“sнιη
    Mar 23 at 19:22













  • 1




    Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
    – DopeGhoti
    Mar 23 at 19:13







  • 1




    Why the loop? sum=$(($1 + $2 + $3 + $4)) is enough IMHO.
    – nohillside
    Mar 23 at 19:14










  • I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
    – Î±Ò“sнιη
    Mar 23 at 19:22








1




1




Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
– DopeGhoti
Mar 23 at 19:13





Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
– DopeGhoti
Mar 23 at 19:13





1




1




Why the loop? sum=$(($1 + $2 + $3 + $4)) is enough IMHO.
– nohillside
Mar 23 at 19:14




Why the loop? sum=$(($1 + $2 + $3 + $4)) is enough IMHO.
– nohillside
Mar 23 at 19:14












I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
– Î±Ò“sнιη
Mar 23 at 19:22





I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
– Î±Ò“sнιη
Mar 23 at 19:22











3 Answers
3






active

oldest

votes

















up vote
2
down vote













If you only want to add 4 numbers, this should be enough:



sum() 
printf '%dn' "$(( $1 + $2 + $3 + $4 ))"



Or if you want to add an arbitrary amount of numbers:



sum() 
local number sum

for number; do
(( sum += number ))
done

printf '%dn' "$sum"






share|improve this answer



























    up vote
    1
    down vote













    Make the shell work for you. And use external utilities when called for.



    #!/bin/bash
    IFS=+
    echo "$*" | bc





    share|improve this answer



























      up vote
      0
      down vote













      There is no need to loop if you know you're always going to get four integers on the command line:



      #!/bin/sh

      sum=$(( $1 + $2 + $3 + $4 ))
      printf 'sum is %dn' "$sum"


      Alternatively, just



      #!/bin/sh

      printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"


      To support an arbitrary number of argument, you will need to loop:



      #!/bin/sh

      while [ "$#" -gt 0 ]; do
      sum=$(( sum + $1 ))
      shift
      done

      printf 'sum is %dn' "$sum"


      This script will iterate over its command line arguments, adding the first one to the sum variable and shifting it off the list of command line arguments, until no arguments are left. The $# variable expansion will expand to the number of command line arguments, while shift will remove $1 from the list, shifting $2 into its place (and $3 into $2 etc.).



      Alternatively:



      #!/bin/sh

      for num do
      sum=$(( sum + num ))
      done

      printf 'sum is %dn' "$sum"


      Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum in turn.



      The for num do loop head may also be written for num in "$@"; do.






      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: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        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%2f433129%2fissues-with-script-for-adding-numbers%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        If you only want to add 4 numbers, this should be enough:



        sum() 
        printf '%dn' "$(( $1 + $2 + $3 + $4 ))"



        Or if you want to add an arbitrary amount of numbers:



        sum() 
        local number sum

        for number; do
        (( sum += number ))
        done

        printf '%dn' "$sum"






        share|improve this answer
























          up vote
          2
          down vote













          If you only want to add 4 numbers, this should be enough:



          sum() 
          printf '%dn' "$(( $1 + $2 + $3 + $4 ))"



          Or if you want to add an arbitrary amount of numbers:



          sum() 
          local number sum

          for number; do
          (( sum += number ))
          done

          printf '%dn' "$sum"






          share|improve this answer






















            up vote
            2
            down vote










            up vote
            2
            down vote









            If you only want to add 4 numbers, this should be enough:



            sum() 
            printf '%dn' "$(( $1 + $2 + $3 + $4 ))"



            Or if you want to add an arbitrary amount of numbers:



            sum() 
            local number sum

            for number; do
            (( sum += number ))
            done

            printf '%dn' "$sum"






            share|improve this answer












            If you only want to add 4 numbers, this should be enough:



            sum() 
            printf '%dn' "$(( $1 + $2 + $3 + $4 ))"



            Or if you want to add an arbitrary amount of numbers:



            sum() 
            local number sum

            for number; do
            (( sum += number ))
            done

            printf '%dn' "$sum"







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 23 at 19:25









            nxnev

            2,4522423




            2,4522423






















                up vote
                1
                down vote













                Make the shell work for you. And use external utilities when called for.



                #!/bin/bash
                IFS=+
                echo "$*" | bc





                share|improve this answer
























                  up vote
                  1
                  down vote













                  Make the shell work for you. And use external utilities when called for.



                  #!/bin/bash
                  IFS=+
                  echo "$*" | bc





                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Make the shell work for you. And use external utilities when called for.



                    #!/bin/bash
                    IFS=+
                    echo "$*" | bc





                    share|improve this answer












                    Make the shell work for you. And use external utilities when called for.



                    #!/bin/bash
                    IFS=+
                    echo "$*" | bc






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 23 at 19:35









                    Wildcard

                    21.9k855153




                    21.9k855153




















                        up vote
                        0
                        down vote













                        There is no need to loop if you know you're always going to get four integers on the command line:



                        #!/bin/sh

                        sum=$(( $1 + $2 + $3 + $4 ))
                        printf 'sum is %dn' "$sum"


                        Alternatively, just



                        #!/bin/sh

                        printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"


                        To support an arbitrary number of argument, you will need to loop:



                        #!/bin/sh

                        while [ "$#" -gt 0 ]; do
                        sum=$(( sum + $1 ))
                        shift
                        done

                        printf 'sum is %dn' "$sum"


                        This script will iterate over its command line arguments, adding the first one to the sum variable and shifting it off the list of command line arguments, until no arguments are left. The $# variable expansion will expand to the number of command line arguments, while shift will remove $1 from the list, shifting $2 into its place (and $3 into $2 etc.).



                        Alternatively:



                        #!/bin/sh

                        for num do
                        sum=$(( sum + num ))
                        done

                        printf 'sum is %dn' "$sum"


                        Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum in turn.



                        The for num do loop head may also be written for num in "$@"; do.






                        share|improve this answer


























                          up vote
                          0
                          down vote













                          There is no need to loop if you know you're always going to get four integers on the command line:



                          #!/bin/sh

                          sum=$(( $1 + $2 + $3 + $4 ))
                          printf 'sum is %dn' "$sum"


                          Alternatively, just



                          #!/bin/sh

                          printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"


                          To support an arbitrary number of argument, you will need to loop:



                          #!/bin/sh

                          while [ "$#" -gt 0 ]; do
                          sum=$(( sum + $1 ))
                          shift
                          done

                          printf 'sum is %dn' "$sum"


                          This script will iterate over its command line arguments, adding the first one to the sum variable and shifting it off the list of command line arguments, until no arguments are left. The $# variable expansion will expand to the number of command line arguments, while shift will remove $1 from the list, shifting $2 into its place (and $3 into $2 etc.).



                          Alternatively:



                          #!/bin/sh

                          for num do
                          sum=$(( sum + num ))
                          done

                          printf 'sum is %dn' "$sum"


                          Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum in turn.



                          The for num do loop head may also be written for num in "$@"; do.






                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            There is no need to loop if you know you're always going to get four integers on the command line:



                            #!/bin/sh

                            sum=$(( $1 + $2 + $3 + $4 ))
                            printf 'sum is %dn' "$sum"


                            Alternatively, just



                            #!/bin/sh

                            printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"


                            To support an arbitrary number of argument, you will need to loop:



                            #!/bin/sh

                            while [ "$#" -gt 0 ]; do
                            sum=$(( sum + $1 ))
                            shift
                            done

                            printf 'sum is %dn' "$sum"


                            This script will iterate over its command line arguments, adding the first one to the sum variable and shifting it off the list of command line arguments, until no arguments are left. The $# variable expansion will expand to the number of command line arguments, while shift will remove $1 from the list, shifting $2 into its place (and $3 into $2 etc.).



                            Alternatively:



                            #!/bin/sh

                            for num do
                            sum=$(( sum + num ))
                            done

                            printf 'sum is %dn' "$sum"


                            Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum in turn.



                            The for num do loop head may also be written for num in "$@"; do.






                            share|improve this answer














                            There is no need to loop if you know you're always going to get four integers on the command line:



                            #!/bin/sh

                            sum=$(( $1 + $2 + $3 + $4 ))
                            printf 'sum is %dn' "$sum"


                            Alternatively, just



                            #!/bin/sh

                            printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"


                            To support an arbitrary number of argument, you will need to loop:



                            #!/bin/sh

                            while [ "$#" -gt 0 ]; do
                            sum=$(( sum + $1 ))
                            shift
                            done

                            printf 'sum is %dn' "$sum"


                            This script will iterate over its command line arguments, adding the first one to the sum variable and shifting it off the list of command line arguments, until no arguments are left. The $# variable expansion will expand to the number of command line arguments, while shift will remove $1 from the list, shifting $2 into its place (and $3 into $2 etc.).



                            Alternatively:



                            #!/bin/sh

                            for num do
                            sum=$(( sum + num ))
                            done

                            printf 'sum is %dn' "$sum"


                            Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum in turn.



                            The for num do loop head may also be written for num in "$@"; do.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 5 at 19:41

























                            answered May 5 at 18:58









                            Kusalananda

                            102k13201317




                            102k13201317






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433129%2fissues-with-script-for-adding-numbers%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                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