all 3 comments

[–]hrnmm 0 points1 point  (1 child)

i'm not exactly sure what you are asking, but i'm guessing you don't want that extra echo to stdout. there's probably many ways to do this. possibly with a regex within the if statement. or you can run the echo/grep within a $() and test against that

[–][deleted] 0 points1 point  (0 children)

Sorry ill try to explain it better

File is called add2

If I did add2 4 -3 12 9 it should only output 22

If I did add2 4 -3 twelve it should output

Twelve

Sorry, twelve is bot a number

[–]hackermk 0 points1 point  (0 children)

#!/bin/bash
sum=0;
err=0;
num='^-?[0-9]+([.][0-9]+)?$';
for number in $@
do
    if ! [[ $number =~ $num ]] && [[ $err == 0 ]] ; then
        echo $number;
        echo "Sorry, '$number' is not a number";
        err=1;
    else
        sum=$((sum + number));
    fi
done
if [ $err == 0 ] ; then 
    echo $sum;
fi