Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the power in bash

Tags:

bash

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?

like image 477
user2843457 Avatar asked Apr 13 '14 15:04

user2843457


People also ask

How do you find the power of a number in bash?

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.

What does [- Z $1 mean in bash?

$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.

What is if [- Z in bash?

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.

Can you do math in Bash?

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.


1 Answers

The power operator in bash is **

Example:

echo $((2 ** 4))
16
like image 112
user000001 Avatar answered Sep 20 '22 06:09

user000001