Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is this a bug or a feature?
It seems to happen for unquoted alphanumerics.

Running GNU bash, version 4.1.5

for alpha01234; do echo $((++i)); done

output:

1
2
3
4
share|improve this question
add comment

2 Answers 2

up vote 14 down vote accepted

That is a feature of the for compound command, as described by help for:

for: for NAME [in WORDS ... ] ; do COMMANDS; done

Execute commands for each member in a list.

The for loop executes a sequence of commands for each member in a list of items. If in WORDS ...; is not present, then in "$@" is assumed. For each element in WORDS, NAME is set to that element, and the COMMANDS are executed.

So the code you quoted is probably executed in a script which was called with 4 parameters.

share|improve this answer
    
Thanks... The thing that threw me completely was that it happened at the command line.. I actually didn't try it in a script.. I must have used set a b c d at some time recently. I often use set that way in a script for testing.. After restarting the terminal, it has stopped... (makes sense now).. –  Peter.O Nov 24 '11 at 16:56
add comment

for x; do … is a shortcut for for x in "$@"; do …: it iterates over the positional parameters. If it loops four times, it means you have four positional parameters ($1 through $4).

share|improve this answer
add comment

Your Answer

 
discard

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.