11

I'm trying to add 1 number from the command line, and one number as like a default. For example: When user types in the number 50 the script will add 10 ( as the default number).

./script 50
The sum of 50+ 10 is 60. 

This is what I have so far.

echo -n "Please enter a number: " 
read number 
default = 10
sum = $((default + number)) // this line does not seem to work
echo "The sum of $number and 10 is $sum."

Do I have the syntax wrong? I'm not sure if I'm on the right track. Am I adding the numbers wrong? Should I use awk instead?

let sum = $default + $number 
1
  • Just to add, instead of echoing and then the usage of read directly you can type: read -p 'Please enter a number: ' yournumber Jun 16, 2019 at 6:46

3 Answers 3

15

You should not have a spaces inbetween "default = 10" & "sum = $", also default & number should have $ before them to read from the variables.

The script then works as expected for me, when written like below;

#!/bin/bash

echo -n "Please enter a number: " 
read number 
default=10
sum=$(($default + $number))
echo "The sum of $number and 10 is $sum."
2
  • Oh I see! I got it now. It was just a spacing issue. I really over thought this. Thank you for the help!
    – techiegeek
    Dec 12, 2014 at 9:43
  • 3
    Note that you don't need to use the $ inside the arithmetic expression. Eg, try this: num=5;def=10;echo "sum = $((num + def))"
    – PM 2Ring
    Dec 12, 2014 at 9:46
5

This is the fastest way to do what you are asking:

#!/bin/bash
echo "The sum of $1 + 10 is $(($1 + 10))."

Output:

creme@fraiche:~/$ ./script.sh 50
The sum of 50 + 10 is 60.
2
  • 1
    that could be dangerous - try ./script.sh '((PATH=0)+(ANY_OTHER_VARNAME=[ANY INTEGER]))'
    – mikeserv
    Dec 12, 2014 at 13:55
  • @mikeserv Very accurate, however for the exercise I'm not sure if it is a concern. Thank you for your comment regardless. Dec 12, 2014 at 23:17
3

Spaces are causing the errors.

If you want user to input the number when he is prompted as "Please enter a number:", you can use your script with some corrections as:

#!/bin/bash
echo -n "Please enter a number: " 
read number 
default=10
sum=`echo "$number + $default" | bc`
echo "The sum of $number and 10 is $sum."

Check:

./temp.sh
Please enter a number: 50
The sum of 50 and 10 is 60.

If you want the user to input the number as an argument to the script, you can use the script below:

#!/bin/bash
number="$1"
default=10
sum=`echo "$number + $default" | bc`
echo "The sum of $number and 10 is $sum."

Check:

./temp.sh 50
The sum of 50 and 10 is 60.
2
  • 3
    Why bother running bc just to do simple integer arithmetic?
    – PM 2Ring
    Dec 12, 2014 at 9:48
  • @PM2Ring Some day (maybe today), some user will interpret "number" literally. Bash does integers, bc will handle 3.14159, awk will handle 3.14159e+03. Depends on what you want the user experience to be. Oct 26, 2023 at 15:04

You must log in to answer this question.

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