Script loop question

Cameron Hutchison lists at
Thu Nov 12 22:45:47 UTC 2009

=?UTF-8?B?TWFya3VzIFNjaMO2bmhhYmVy?= <ubuntu-users at> writes:

>Oliver Marshall:

>> I have a script that takes an argument (a file name) and performs
>> actions on that file. I want to mod the script so that i can pass, as
>> arguments, multiple filenames and have the script perform the same
>> set of commands on each file in turn, like this;
>> ./ file1 file2 file3
>> etc.
>> I guess it's bringing the two parts of the loop and the arguments
>> together to loop through each argument in turn, but that's where I
>> get stuck.

>for f in "$@"; do
>  echo $f

for f ; do
  echo "$f"

Two things:
1) 'in "$@"' is redundant. It is the default in a for loop
2) always quote expansions - i.e. "$f", so that spaces are properly retained

More information about the ubuntu-users mailing list