Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise to the power in shell

How do you raise m to the power of n? I've searched for this everywhere. What I found was that writing m**n should work, but it doesn't. I'm using #!/bin/sh.

like image 727
Linas Avatar asked Oct 28 '12 18:10

Linas


People also ask

How do you power in bash?

To execute a Power Bash, simply hold the action button instead of clicking it once, just like performing a Power Attack with any melee weapon. The Power Bash staggers enemies for a noticeably longer period of time than the regular bash does, in addition to doing more damage.

What does $() mean in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What is $? == 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.

What does $$ do in shell?

The $$ variable is the PID (Process IDentifier) of the currently running shell. This can be useful for creating temporary files, such as /tmp/my-script. $$ which is useful if many instances of the script could be run at the same time, and they all need their own temporary files.


2 Answers

I would try the calculator bc. See http://www.basicallytech.com/blog/index.php?/archives/23-command-line-calculations-using-bc.html for more details and examples.

eg.

$ echo '6^6' | bc 

Gives 6 to the power 6.

like image 195
Brian Agnew Avatar answered Sep 26 '22 21:09

Brian Agnew


Using $n**$m really works indeed. Maybe you don't use the correct syntax to evaluate a math expression. Here's how I get the result on Bash:

echo $(($n**$m)) 

or

echo $[$n**$m] 

The square brackets here are not meant to be like the test evaluator in the if statement so you can you use them without spaces too. I personally prefer the former syntax with round brackets.

like image 36
Gianni Avatar answered Sep 22 '22 21:09

Gianni