0

I have this working and simple code to echo positional parameters VALUES

for i
do
echo "Argument = $i "
done

Here is the actual output from the scrip

Argument = --atleast-pkgconfig-version
Argument = 0.9.0

It does the job, but I have been trying to figure out how to ADD an index to the echo output.

This does not work

cnt=1
for i
do
echo "Argument $cnt = $i "
((cnt++))
done

The current code does the job, the index would be just icing on the cake - like this:

Argument #1 = --atleast-pkgconfig-version
Argument #2 = 0.9.0

Any ideas?

Update #1

I am posting this as a copy from text editor, basically to bypass „editing in 5 minutes „ time limit.

I did run both codes, both in #! /bin/sh scripts. One is run directly in main script, the second is in script called from the main – as indicated. Both codes run fine in MAIN script, but the first code, the one I have originally posted as having an issue, does not run in secondary script.

Yes, the “more portable” code solves the issue – it runs correctly in desired script. It still does not answer the original question – why does it fail with “not found” error.

MAIN script
--exists glib-2.0                    0
--modversion glib-2.0                2.54.3
--exists --print-errors glib-2.0 >= 2.28    0
First code  - OK 
Argument #1 = dummy
Argument #2 = mt
Second code OK 
Argument #1 = dummy
Argument #2 = mt
Secondary script

First code – FAILS

/usr/bin/arm-linux-gnueabihf-pkg-config: 19: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
Argument #1 = --print-errors
/usr/bin/arm-linux-gnueabihf-pkg-config: 19: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
Argument #1 = glib-2.0 >= 2.28
/usr/bin/a
Second code – runs fine
Argument #1 = --exists
Argument #2 = --print-errors
Argument #3 = glib-2.0 >= 2.28
First code
cnt=1
for i do
    echo "Argument #$cnt = $i"
    ((cnt++))
done
Second code
count=1
for arg do
    printf 'Argument #%d = %s\n' "$count" "$arg"
    count=$(( count + 1 ))
done
1
  • 2
    j=0; for i do printf "arg#$((j=j+1)) = %s\n" "$i"; done. And no, you cannot use ++j in all shells.
    – user313992
    Apr 22, 2019 at 14:03

1 Answer 1

2

Your second example just needs an # in the output string to generate the correct output when run in bash:

cnt=1
for i do
    echo "Argument #$cnt = $i"
    ((cnt++))
done

Note that this would not generally work if executed with /bin/sh, especially not if /bin/sh is a shell (like dash) that does not understand the ((...)) syntax for arithmetic evaluation. The dash shell would generate a cnt++: not found error for the ((cnt++)) code above as it tries to execute the cnt++ string inside two sets of sub-shells. If you need to use the code above, make sure that it is executed by e.g. bash by using a #!/bin/bash line at the top of the script.

For maximum portability, you would use

count=1
for arg do
    printf 'Argument #%d = %s\n' "$count" "$arg"
    count=$(( count + 1 ))
done

This additionally avoids issues with arguments containing backslashes if the xpg_echo shell option happens to be enabled in bash, and it uses POSIX syntax for incrementing the counter.

3
  • Unfortunately it does not work , at least in shell.
    – anne
    Apr 23, 2019 at 16:36
  • Unfortunately it does not work , at least in shell. I believe the "cnt" is defined "outside" the "for" loop, hence "not found" in do /done code. Perhaps the "for" loop should be declared with both i and cnt as variables? My guess would be that bash / shell do not have that option, I cannot get pass syntax error.
    – anne
    Apr 23, 2019 at 16:42
  • @anne Are you talking about the first piece of code in the answer specifically? That code would work in bash and I've tested it. If you execute the script with /bin/sh instead of bash, then it may well fail to work as expected (/bin/sh is not bash on many systems). The second piece of code would work in any sh-shell (including bash). In both codes, the counter variable would need no special declaration inside the loop.
    – Kusalananda
    Apr 23, 2019 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .