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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Linas
  • 560
  • 1
  • 5
  • 16

6 Answers6

41

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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Awesome! Used to do my own math functions in bash, created a lot of useless ugly code! Thanks! Didn't know about bc. – Thibault D. Jun 23 '14 at 15:42
  • if you can spare 40 more megabytes to install it, [PARI/GP](http://pari.math.u-bordeaux.fr/) does the trick and can handle bigger numbers, is faster, and has richer computations. to answer the original question: `echo 6^6 | gp -q` – éclairevoyant Sep 06 '22 at 10:22
33

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.

Gianni
  • 347
  • 3
  • 2
  • 1
    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. – josch Jul 22 '17 at 19:21
  • 3
    This is the answer that I was looking for and that I would accept. Thank you @Gianni – Alexx Roche Oct 13 '17 at 09:11
  • 3
    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, `(( ... ))`. – codeforester Oct 20 '18 at 19:44
  • kind of this answer was already in the original question ! – sol Aug 05 '22 at 13:47
  • 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. – rethab Jul 21 '23 at 07:03
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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
4

You might use dc. This

dc -e "2 3 ^ p"

yields

8
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
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.

markroxor
  • 5,928
  • 2
  • 34
  • 43
-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 
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32