How do you add a number as a command line argument?

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."

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.

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.

Tags:

Bash