0

Just saw this in a script at my internship. I've googled but for the life of me I cannot figure out what kind of for loop this is.

 # Declare variables for use
 35   args=$*
 36   noofargs=$#
 37   isjavapassed="false"
 38   isjavavalid="false"
 39   argsarray=""
 40   d64option=""
 41
 42  varcount=0;
 43  for tempvar
 44  do ....

how does this for loop work? it is literally just for tempvar? where is the terminating condition or the list it iterates over? There is nothing above this but comments. Any help is much appreciated. Thank you.

2 Answers 2

3

From the Bash Reference Manual section on Looping Constructs:

The syntax of the for command is:

for name [ [in [words …] ] ; ] do commands; done

Expand words, and execute commands once for each member in the resultant list, with name bound to the current member. If ‘in words’ is not present, the for command executes the commands once for each positional parameter that is set, as if ‘in "$@"’ had been specified (see Special Parameters). The return status is the exit status of the last command that executes. If there are no items in the expansion of words, no commands are executed, and the return status is zero.

So that's a case where "in words" is missing and so it loops over the positional parameters.

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

Comments

1

Basic for syntax is:

for arg in [list]
do
 command(s)...
done

Omitting the in [list] part of a for loop causes the loop to operate on $@ -- the positional parameters.

Try running the below script with and without arguments:

for a
do
 echo -n "$a "
done

SOURCE Example 11-6

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.