16

I am trying to add two floating point numbers together using shell script. I have tried this:

#!/bin/bash
if [ $# != 2 ]; then
    echo "2 arguments are required "
    exit
else
    x=$1
    y=$2
    sum = $x + $y
    echo ` sum = $sum | bc `
fi

When I provide two arguments to the command line, for example:

bash filename.sh 2.4 5

... it gives me an error: [ 2 != 2 ] command not found

6
  • Could you please provide an example of how you are running the script?
    – user14755
    Commented Apr 21, 2017 at 0:27
  • bash filename.sh 2.4 5 Commented Apr 21, 2017 at 0:36
  • Try to execute it like ./filename.sh 2.4 5 Commented Apr 21, 2017 at 0:42
  • What is the output of type -a bash ?
    – user14755
    Commented Apr 21, 2017 at 0:54
  • Are you modifying the IFS variable?
    – user14755
    Commented Apr 21, 2017 at 0:55

7 Answers 7

9
else
    echo -n "sum = "
    echo "$1 + $2" | bc
fi

will fix the second half of your problem that you have not yet got to. Your first problem is a mystery. "[" is a built-in command so unless there are quotation marks you are not showing us I can't see how it can take [ $# != 2 ] as a single word.

6

Using bc:

    #!/bin/bash
    n="$@" 
    bc <<< "${n// /+}"

Supposing the script is called add, or for those who prefer easily pasted code try this workalike shell function : add() { n="$@"; bc <<< "${n// /+}"; }; both function and script work like this:

add 3.2 5.5
add 3.2 5.5 8.9
add {1..51}.{12..89}

The curly braces use bash brace expansion to create about 4000 strings that bc interprets as decimal numbers ranging from 1.12 to 51.89.

Output:

8.7
17.6
105436.89

Note how there's no need to check for two arguments:

  • it can add one or more arguments,

  • no arguments returns no output.

  • it ignores ordinary strings, so add 5 6.7 abc edf 9 returns 20.7.

  • it returns a syntax error if a number isn't right, e.g.: 9z, 5.6.7, 8.., etc.

5

Use this to add two floating numbers.

echo 12.8 12.2 | awk '{print $1 + $2}'

Result:- 25

Just replace the numbers with your variables.

You may use

awk "BEGIN {print 12.8+12.2; exit}"

1
  • best answer using a program that is still common across distros
    – duanev
    Commented May 17, 2021 at 22:34
3

Your script:

#!/bin/bash
if [ $# != 2 ]; then
    echo "2 arguments are required "
    exit
else
    x=$1
    y=$2
    sum = $x + $y
    echo ` sum = $sum | bc `
fi
  1. All variable substitutions should be double-quoted: http://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
  2. Arithmetic comparison for inequality is done with [ ... -ne ... ].
  3. Diagnostic output, i.e. errors and warnings, should go to standard error.
  4. When exiting on an error condition, a non-zero exit status should be returned to the calling shell.
  5. Assignments do not accept spaces around =.

With these things in mind, your code becomes

#!/bin/sh

if [ "$#" -ne 2 ]; then
    echo >&2 'Expected two arguments'
    exit 1
fi

printf 'sum = %f\n' "$( printf '%f + %f\n' "$1" "$2" | bc )"

Alternatively, with a couple of bash extensions:

#!/bin/bash

if (( $# != 2 )); then
    echo >&2 'Expected two arguments'
    exit 1
fi

printf 'sum = %f\n' "$( bc <<<"$1 + $2" )"

With intermediate variables:

#!/bin/bash

if (( $# != 2 )); then
    echo >&2 'Expected two arguments'
    exit 1
fi

x="$1"
y="$2"
sum="$( bc <<<"$x + $y" )"
printf 'sum = %f\n' "$sum"

Modify the printf formatting string to suit your needs. If you, for example, want two decimals, use %.2f instead of %f.

0
if [ "$#" != 2 ]; then
   echo "2 arguments are required"; exit 1
else
   x=$1 y=$2
   sum="[sum=]n $x $y + 2k p"
   echo "$sum" | dc
fi

Result:

sum=7.4

Explanation:

We use the `dc` calculator by placing the two operands on the stack and
adding the two top of stack elements. And prior to adding, we place a
string `sum=` on the stack, and immediately print it which as a side effect
also removes it from the stack. The precision of the results is set to 2.
0

One can use gnuplot if it is avaliable (similar as awk)

a=1.234
b=5.678
gnuplot -e "print $a+$b"

However, such a solution seems to be a little bit overkill :)

0
#!/bin/bash

if [ $# -ne 2 ]; then
    echo "2 arguments are required"
else
    x=$1
    y=$2
    sum="$x + $y"
    echo "sum = `bc <<< $sum`"
fi

Saving the script to sum.sh and executing it gives this output

chmod +x sum.sh
./sum.sh 2.45 2.55
sum = 5.00
6
  • 1
    The echo in backticks is a bit of a code smell. Somehow printf would feel less convoluted here.
    – tripleee
    Commented Apr 21, 2017 at 4:32
  • @tripleee Fixed. I am not a fan of using printf as on many default Linux installs printf was missing.
    – GMaster
    Commented Apr 21, 2017 at 5:48
  • 2
    Huh? It was specified by POSIX way, way back. Which Linuxes are missing it?
    – tripleee
    Commented Apr 21, 2017 at 5:55
  • 2
    @GMaster Find those default Linux installs and report to their maintainers that they are seriously broken. printf is a POSIX utility.
    – Kusalananda
    Commented Apr 21, 2017 at 6:28
  • @GMaster assuming you already chmod'ed it. Commented Sep 8, 2020 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .