Yurkee
Yurkee

Reputation: 873

bash passing value as a parameter calculation

I wrote small bash script

for (( j=10;j<20; j++ ))
    do
    ./b5 $j $[ $1 * 3 ]
    done

Which should execute program b5 and send two parameters, $j and $1 multiplied by 3.

When I try to run it, i get:

 * 3 : syntax error: operand expected (error token is "* 3 ")

How should one do it?

Upvotes: 0

Views: 1411

Answers (3)

Yurkee
Yurkee

Reputation: 873

To be honest, both options work:

  • $(( $1 * 3 ))
  • $[ $1 * 3]

but he problem was $1 was not initialized. My mistake.

Upvotes: 1

sat
sat

Reputation: 14949

You have to use $((..)) for doing arithmetic expression.

Instead,

./b5 $j $[ $1 * 3 ]

to

./b5 $j $(($1 * 3))

Upvotes: 1

agilob
agilob

Reputation: 6223

You should do expr $1 \* 3 to multiply variable by a digit.

Upvotes: 1

Related Questions