Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

for (( i=1; i<=3; i++ )); do
for (( j=1; j<=3; j++ )); do
for (( k=1; k<=3; k++ )); do
echo $i$j$k
done
done
done

Will the above code work on a BOURNE shell?

As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using the command

sh lol.sh

then I am using the Bourne Shell. Am I right with this concept?

If yes, then the above code won't run (as I tried to run it) BUT it runs when i run it as...

bash lol.sh

So my question is that :

  1. Am I correct that sh lol.sh will run it using bourne shell ?

  2. And can for loop be written in the way it's written in the code above in the bourne shell or

for i in 1 2 3

is the only possible syntax of for loop in the bourne shell?

No, that will not work in a generic Bourne shell. Generic Bourne shells don't even have math, you have to use the expr external.

The latter is what should be used for a generic bourne shell, yes. Or something like

I=1
while [ $I -le 3 ]
do
        I=`expr $I + 1`
done

If bourne shells doesn't have math then how am I able to execute statements like

 var=$(($1-1))

And is it possible to find x to the power of y using expr function (or in any possible way in bourne shell except "for" loop) where x and y are two numbers read from the terminal?

By using the expr external, like I said.

VAR=`expr $VAR - 1`

Certainly, by running a loop which multiplies x against itself y times.

Is this homework?

Thank you for your time but if you would read my last post carefully and not skim over it... you'd see that I said "except for loop" which implies that I already know how to do it with a for loop but I was asking you for some other method "if it exits".

Second thing, I KNOW that it can be done by the expr command but I asked you that WHY I am getting away with using a command like $((...mathematical expression...)) if I am working in the bourne shell WHICH implies that I ALREADY understood that expr can be used.
I do understand if "...am i able to..." couldn't really pass my message along clearly but I think "....except for loop...." was mentioned pretty neatly.

Again, thank you for your time and effort but kindly re read the post before tagging my genuine problem as "homework".

On some systems Bourne shell (sh) is actually Bash (bash).
Compare the sizes/links of /usr/bin/sh and /usr/bin/bash

As far as power is concerned:

$ nawk 'BEGIN{ print 2^3}'
8

A simple "no" would have sufficed. I didn't "tag" your post as homework, I asked.

Most of the time I ask, the answer is "yes". Your question looked contrived, as homework frequently is, because calculating X^Y isn't something that makes much sense to do in a shell. If it was homework, I'd have been breaking the rules by giving you an answer. I've been burned several times by blatant cheaters, too.

If I ask someone what their shell is, and they tell me "Bourne", I ask "which Bourne" because "bourne" means it supports at least the Bourne shell features, not that it's the One and Only True Original Bourne Shell. Some systems -- particularly Linux ones -- use Bash as their Bourne shell, many others use KSH, and a few poor Solaris folks are stuck using an actual ancient pre-POSIX Bourne shell which hasn't been improved since the dawn of UNIX.

If $(( )) works, you're either in bash or ksh.

I don't know a simple syntax for taking a number to a power in either. If I had to do so in basic Bourne, I'd probably pipe "$x^$y" into bc. If you try and do that in $(( )), it's a bitwise exclusive-or, not x to a power, since $(( )) mostly emulates the C expression syntax, and C has no operator for power.

1 Like

Okay.

Thanks again.