Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I run this script I get this error:

./myscript.sh: 16: arithmetic expression: expecting primary: "1 ** 1"

When I run this shell script with bash, as in #! /bin/bash on the first line, the math works properly; unfortunately I need to use /bin/sh. What am I doing wrong? I'm on Linux Mint if that matters.

#! /bin/sh

while [ $x -le 10 ]
    while [ $y -le 10 ]
        echo $(($y ** $x))"   \c"
        y=`expr $y \+ 1`
    x=`expr $x \+ 1`
share|improve this question
up vote 5 down vote accepted

Standard shell arithmetic only allows integer arithmetic operations. This doesn't include ** for exponentiation, which bash has as an extension.

Integer exponentiation is easy enough to implement as a shell function (though you'll run into wraparound soon).

pow () {
    set $1 $2 1
    while [ $2 -gt 0 ]; do
      set $1 $(($2-1)) $(($1*$3))
    echo $3

As an aside, why use expr here? Shell arithmetic can do addition.

share|improve this answer
Wow thank you so much. Another quick question if you don't mind... when writing a bash shell script, what is the bash version of \c... when I put that in it echo's it verbatum instead of the next echo being on the same line. – whuff739 Feb 23 '11 at 22:09
@whuff739: echo -n. If you want to be portable, use printf %s 'print\this!exactly+with%no$newline*at;the&end'. – Gilles Feb 23 '11 at 22:14
You should also check out the bc program if you want to stuff other than basic add/multiply/subtract/divide. Example: echo $(echo '2^3' | bc) (using ksh here) – Nitrodist Feb 23 '11 at 22:16
you're the man. thanks. – whuff739 Feb 23 '11 at 22:18
@Nitrodist, I'll definitely look into that, thanks a lot. – whuff739 Feb 23 '11 at 22:18

I think you're out of luck, as the ** exponent operator isn't standard for /bin/sh. You can use bc, though: echo "$y ^ $x" | bc.

share|improve this answer

The POSIX shell apparently does not have an exponentiation operator. You can roll your own:

pow() {
    local x y res i
    while [ $i -le $y ]; do
        res=$(( res * x ))
        i=$(( i + 1 ))
    echo $res

while [ $x -le 10 ]; do
    while [ $y -le 10 ]; do
        echo "$(pow $y $x)   \c"
        y=$(( y + 1 ))
    x=$(( x + 1 ))
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.