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
.
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.
echo 6^6 | gp -q
–
Accentuate 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.
echo $((n ** n))
will work as well - no need for $
to expand variables inside the arithmetic expression, (( ... ))
. –
Abbe $((2**100))
gives 0 on my machine. Using bc
works with larger numbers. –
Kallman $((2**62))
is fine - $((2**63))
produces a negative number: presumably overflowing 64-bits. –
Exoskeleton 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
$((7**5))
. –
Pentaprism You might use dc. This
dc -e "2 3 ^ p"
yields
8
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.
#! /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
© 2022 - 2024 — McMap. All rights reserved.
m**n
is a bash feature that is not available in sh – Parnassian