Raise to the power in shell
Asked Answered
S

6

37

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.

Steamboat answered 28/10, 2012 at 18:58 Comment(1)
because m**n is a bash feature that is not available in shParnassian
M
40

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.

Moynihan answered 28/10, 2012 at 19:1 Comment(3)
Awesome! Used to do my own math functions in bash, created a lot of useless ugly code! Thanks! Didn't know about bc.Jeffreys
if you can spare 40 more megabytes to install it, PARI/GP does the trick and can handle bigger numbers, is faster, and has richer computations. to answer the original question: echo 6^6 | gp -qAccentuate
Downvoted why ?Moynihan
M
34

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.

Muskogee answered 11/2, 2015 at 21:15 Comment(5)
This question asked for a solution for shell which means POSIX shell. Your solution does not work in a POSIX compliant shell like dash but just happens to work in more featureful shells like bash or zsh.Edora
This is the answer that I was looking for and that I would accept. Thank you @MuskogeeFarrington
Note that this is limited to integers only - Bash can't handle non-integer arithmetic. Also, echo $((n ** n)) will work as well - no need for $ to expand variables inside the arithmetic expression, (( ... )).Abbe
Careful with this if you're dealing with large numbers. For example, $((2**100)) gives 0 on my machine. Using bc works with larger numbers.Kallman
Yes, this is very nice, but while $((2**62)) is fine - $((2**63)) produces a negative number: presumably overflowing 64-bits.Exoskeleton
T
9

using bc is an elegant solution. If you want to do this in bash:

$ n=7
$ m=5

$ for ((i=1, pow=n; i<m; i++)); do ((pow *= n)); done
$ echo $pow
16807

$ echo "$n^$m" | bc  # just to verify the answer
16807
Teens answered 29/10, 2012 at 11:32 Comment(2)
In bash you can also use $((7**5)).Pentaprism
well, then. that's even easier.Teens
P
4

You might use dc. This

dc -e "2 3 ^ p"

yields

8
Pentaprism answered 28/10, 2012 at 19:3 Comment(1)
this might interest ppl interested in dc unix.stackexchange.com/a/124524/30352Padilla
L
1

My system admin didn't install dc so adding to other correct answers, I bet you have not thought of this -

a=2
b=3
python -c "print ($a**$b)"
>> 8

works in bash/shell.

Lobate answered 20/10, 2018 at 19:16 Comment(0)
P
-1
#! /bin/bash
echo "Enter the number to be done"
n=2
read m
let P=( $n**$m )
echo "The answer is $p"

ANSWER

Enter the number to be done
3
The answer is 8 
Pushball answered 30/9, 2019 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.