views:

61

answers:

2

I try

echo 10**2 

it prints 10**2. How to make it work?

+5  A: 

You can do:

let var=10**2   # sets var to 100.

or even better and recommended way:

var=$((10**2))  # sets var to 100.

If you just want to print the expression result you can do:

echo $((10**2)) # prints 100.

For large numbers you might want to use the exponentiation operator of bc as:

bash:$ echo 2^100 | bc
1267650600228229401496703205376

If you want to store the above result in a variable you can again use the $(()) syntax as:

 var=$((echo 2^100 | bc))  
codaddict
Instead of `$(( ... ))` it's also possible to use `$[ ... ]`. I find the later visually more appealing but that's just my taste. Don't know if there are any differences between the two, though. It seems they behave the same.
DarkDust
Alberto: all this parenthesis, lets and $ are needed because bash, being a simple command-line interpreter, does better assuming everything is a string and should be treated as such, unless explicitly indicated otherwise. Saludos!
Santiago Lezica
+3  A: 

various ways

Bash

x=2
echo $((x**2))

Awk

awk 'BEGIN{print 2**2}'

bc

echo "2 ^ 2" |bc

dc

dc -e '2 2 ^ p'
ghostdog74