0

Recently I found a part of code which confuses me:

typeset _Var=''

for _Var
do
   ... a command
done

can anyone explain how this loops through an empty list of values is working?

thanks a lot for any post.

3
  • Actually, it does not !! Commented Dec 28, 2015 at 14:08
  • The empty string in typeset _Var='' is a red herring. The for keyword will provide _Var with other values, explicit with the in keyword, or implicit with out. See below. Commented Dec 28, 2015 at 22:03
  • That’s because for x + newline(!) + do is equivalent to POSIX shell for x in "$@"; do. Note that you cannot use for x; do because some shells misparse that. Source: GNU autoconf texinfo manual, section about portable shell. Commented Dec 29, 2015 at 15:02

1 Answer 1

2

The construct

for var
do
    echo something
done

without the in keyword will default to the list of positional parameters, e.g., $1, $2, etc.

So your script will execute the loop body once for each separate argument on the command line.

The snippet above invoked as ksh testscript a b c will output:

something
something
something
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.