The loop was executed $count times

#!/bin/sh
count=0
for i in 2 4 6
do
   echo "i is $i"
   count='expr $count + 1'
done
echo "The loop was executed $count times"

with these scripts
my output is :
i is 2
i is 4
i is 6
The loop was executed expr $count + 1 times

What should I do to get the value instead of 'expr $count + 1?

Thanks for willing to help..

Change

count='expr $count + 1'

to

count=$(expr $count + 1)

Please use backquote ` instead of single quote ' at the code
Instead of

count='expr $count + 1'

It should be

count=`expr $count + 1`

count='expr $count + 1'

correct one is

 
count=`expr $count + 1`

Why not use the shell built-in:

count=$((count+1))

:slight_smile: which can be written

((count++))

or

let count++

:slight_smile:

That is bash/ksh only, it is not POSIX compliant, which is why I suggested count=$((count+1))

---------- Post updated at 11:22 ---------- Previous update was at 11:20 ----------

let and ++ are ksh/bash only, not POSIX

Instead of ++ you can use a direct assignment in POSIX like this:

i=0
while [ $((i+=1)) -le 5 ]; do
  echo $i
done

@Scruti :

I have just tried the following look ... funny behaviour :slight_smile: :

$ exec /usr/xpg4/bin/sh
$ let i++
/usr/xpg4/bin/sh: i++: bad number
$ let i+=1
/usr/xpg4/bin/sh: i+=1: bad number
$ i=0
$ let i++
/usr/xpg4/bin/sh: i++: more tokens expected
$ let i+=1
$ echo $i
1
$

... it just demonstrates what you said :wink:

Just a small correction: I believe POSIX suggests the following syntax:

count=$(($count+1))

Since /usr/xpg4/bin/sh on Solaris is actually an adapted Korn shell, it may well understand "let". It is however not part of POSIX.

OMG ... so on my machine, where should i go to find a POSIX shell ??? :smiley:
Lol

I haven't come across a situation where leaving out the $-sign did not work. Also, I found this:

Arithmetic Expansion

---------- Post updated at 12:03 ---------- Previous update was at 12:00 ----------

It is a POSIX compliant shell and so are ksh93 and bash. It is just that it may have some additional features that are not POSIX compliant.. If you want your code to work universally in all POSIX compliant shells you should avoid using those features.

1 Like

Yeah i meant : on my machine, where should i go to find a pure POSIX shell (no additional feature) ...( that was to test the "let" behaviour)

But anyway, i think that ... unless we meet special case (with strong cross system compatibility constraints and requirements) , i prefer to take advantage of the new fonctionalities offered by ksh or bash even if not purely POSIX compliant.

:slight_smile:

I do not think there is such a thing a pure POSIX compliant shell without additional features. Some shells aim to be. Of the shells I worked with dash comes closest I think.. I too prefer to take advantage of modern features and if I am working in an environment where that is not the problem, I will. Or if it is acceptable to compile or install superior ksh93 for every platform available, then fine.

But otherwise I restrict myself to POSIX compliant code, whenever possible.

A special case is this forum. To let solutions be of use in as many situations as possible, it is good practise to use POSIX compliant code, whether that is shell, awk, sed etc. If it is not, then it is good to mention that fact, for example you can state that it is GNU only of bash, ksh93, ksh, zsh etc.. That way you helping people who would otherwise waste time trying to reuse your code on their platform only to find out that it does not work..

1 Like

Hm, I stand corrected.

It's added to SUS v3, that phrase is missing in v2.

When I posted the wrong correction, I didn't even checked, I was really sure, because I remember it's been pointed out many times by shell experts (or it's just my memory not serving right :)).

Thank you!

---------- Post updated at 01:25 PM ---------- Previous update was at 12:34 PM ----------

OK,
trying to understand where my confusion came from, I searched my mail archive :slight_smile:

I suppose that the following information will be of interest only to limited number of forum members:

So:

The full text is here.

1 Like

Perhaps it is good to note that IMO this does not contradict what I wrote above, since recent POSIX says that the dollar sign can only be left out if the shell variable x contains a value that forms a valid integer constant, which is not the case in this example.

dash:

$ i="1 + 1"
$ echo $((i))
dash: Illegal number: 1 + 1
$ echo $(($i))
2

I wouldn't use arithmetic expansion for strings, so for me this is normally not relevant, but it is good to know...

I agree,
the main point was that there are old shells that require the preceding dollar sign.

---------- Post updated at 03:11 PM ---------- Previous update was at 02:41 PM ----------

Just to add that most probably the statement in SUS v3 that you indicated was added after S.C. expressed his opinion (see the next post in the c.u.s. thread by one of the authors of SUS.