What's the correct way to do this "period=period+($0*1000)" in shell?
I have a .sh file that has this.
I'm passing an integer to the file in $0
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
and I get
The arguments to a script start at $1, not $0.
To do maths, you need arithmetic expansion:
You can also use
and many other ways.
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:
The parentheses in ($1*1000) are not really needed here, as multiplication precedes addition, but I added them to address your comment.