To calculate $1*$1*$1
#!/bin/bash
volume=$1*$1*$1
echo "The volume of the cube is"$volume
In terminal, it shows enter image description here
How can I fix this?
To calculate $1*$1*$1
#!/bin/bash
volume=$1*$1*$1
echo "The volume of the cube is"$volume
In terminal, it shows enter image description here
How can I fix this?
Use $(( ))
to evaluate arithmetic expressions in bash
.
volume=$(($1 * $1 * $1))
You can also use the let
command:
let volume=$1*$1*$1
or you can use (( ))
to execute an arithmetic statement
((volume = $1 * $1 * $1))
What you wrote was being treated as a filename wildcard.
You also need to provide an argument to the script, this will be used as $1
.
$ bash testarith.sh 5
The volume of the cube is 125