What's the correct way to do this "period=period+($0*1000)" in shell?

image

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.

Ask AI
#1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 #16 #17 #18 #19 #20 #21 #22 #23 #24 #25 #26 #27 #28 #29 #30 #31 #32 #33 #34 #35 #36 #37 #38 #39 #40 #41 #42 #43 #44 #45 #46 #47 #48 #49 #50 #51 #52 #53 #54 #55 #56 #57 #58 #59 #60 #61 #62 #63 #64 #65 #66 #67 #68 #69 #70