Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

linux shell: for arg; do

Reading LTP shell code it uses strange for loop syntax:

for arg; do
    TCID="${TCID}_$arg"
done
  1. How does it takes arguments? I’d expect it loops over $arg, separating with $IFS, but when trying $ arg="aa bb"; for arg; do echo $arg; done and it prints nothing.
  2. Is it a bashism?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s a special way of handling command-line options using for loop.

It’s equivalent to:

for arg in "$@"; do
    TCID="${TCID}_$arg"
done

It’s not specific to bash. It’s defined in POSIX:

The for Loop

The for loop shall execute a sequence of commands for
each member in a list of items. The for loop requires that the
reserved words do and done be used to delimit the sequence of
commands.

The format for the for loop is as follows:

for name [ in [word … ]] do
    compound-list
done

First, the list of words following in shall be expanded to generate a
list of items. Then, the variable name shall be set to each item, in
turn, and the compound-list executed each time. If no items result
from the expansion, the compound-list shall not be executed. Omitting:

in word

shall be equivalent to:

in "$@"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading