2

I have a .sh file that has this.

I'm passing an integer to the file in $0

...
period=20000
period=period+($0*1000)
echo "$period"
...

What I'm trying to do is period = period + ($0 * 1000) The second line is giving me a syntax error and I want to know how to do this correctly.

I've tried using (($0*1000)) but no difference.

I've also tried

smth=1000*$3
period=period+smth

and I get

20000000+1000*
New contributor
shimomaru is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

2 Answers 2

4

The arguments to a script start at $1, not $0.

To do maths, you need arithmetic expansion:

period=20000
(( period += $1 * 1000 ))

You can also use POSIX-compatible

period=$((period+$1*1000))

and many other ways.

4
  • Thank you for your response. Id like to know if I can do something like this period=$((period+$(($1*1000)) )) to make sure that $1 * 1000 executes first. Just like how if I wanted period + $1 to execute first.
    – shimomaru
    Commented 2 days ago
  • 1
    In arithmetic, multiplication always happens before addition. But you can alwyas use normal single parentheses like $(( (period + 2) * 3 )).
    – choroba
    Commented 2 days ago
  • 1
    (FWIW, the version with $(( … )) is POSIX-compatible, and so will work in all POSIX shells; the version with (( … )) is bash-specific.)
    – gidds
    Commented yesterday
  • 1
    note that the command line arg in $1 is expanded as-is before arithmetic evaluation, so if someone passes an arithmetic expression (instead of a plain number) the resulting expression may not be what you expect. E.g. if the arg is 1+2, then the expression evaluated is also period+1+2*1000, with only the 2 multiplied. To avoid that, either put parenthesis around the expansion (though the arg could still contain something like 1)+(2), or assign it to a temporary variable first, e.g. arg=$(( $1 )); period=$((period + arg * 1000))
    – ilkkachu
    Commented 21 hours ago
3

You mention that $0 is an integer (I guess you meant $1, because $0 is the script itself, but anyway), so @choroba's answer is great. Generalizing, if $1 could also be a float, you could use bc to make the calculation, because $((...)) can only do integer math. So, you could use this:

period=$(echo "$period+($1*1000)" | bc)

The parentheses in ($1*1000) are not really needed here, as multiplication precedes addition, but I added them to address your comment.

3
  • 1
    You'll need to quote the expression if you have parenthesis in it. Otherwise, it will throw a syntax error. period=$(echo "$period+($1*1000)" | bc)
    – Dan
    Commented 2 days ago
  • 1
    @Dan Ok, you were right! The internal parentheses do cause syntax error when the command runs as a script, but the command works with unquoted internal parentheses when run in the shell (with a number instead of $1). Thanks for catching this and letting me know! :) Commented yesterday
  • Also, I'll never learn to ALWAYS USE QUOTES IN BASH (well, almost)! Commented yesterday

You must log in to answer this question.

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