I'm pretty new to bash scripting. I'm trying to work out calculations, and what I'm specifically trying to do is write a script that allows me to enter a parameter, and that my script calculates the power of 2 to that parameter.
So say I would try
bash scriptname 3
My script would calculate 2^3=8
I'm trying with
(( 2 ^ $1 ))
but that's not doing anything. Is there a command to calculate the power of something that I'm not aware of?
Here we will see how to get the number A raise to the power B using bash script. The logic is very simple. We have to use the '**' operator or power operator to do this.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
In both cases, the -z flag is a parameter to the bash's "test" built-in (a built-in is a command that is built-into the shell, it is not an external command). The -z flag causes test to check whether a string is empty. Returns true if the string is empty, false if it contains something.
Math and arithmetic operations are essential in Bash scripting. Various automation tasks require basic arithmetic operations, such as converting the CPU temperature to Fahrenheit. Implementing math operations in Bash is simple and very easy to learn. This guide teaches you how to do basic math in Bash in various ways.
The power operator in bash is **
Example:
echo $((2 ** 4))
16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With