How to use the file names passed as input to shell script using loop statements?

61 views Asked by At

I am passing random number of files to a shell script and trying to print it using a for loop. I tried something as below.

cat >> script.csh
for (( i=1; i<=$#; i++ ));
do
echo $i
done

so if I do ./script.csh xyz.txt abc.txt pqr.txt

I am expecting the output to be

xyz.txt

abc.txt

pqr.txt

But I am getting the output as

1

2

3

Any help is much appreciated. Thanks

1

There are 1 answers

0
Cyrbil On BEST ANSWER

You can iterate through program arguments with this syntax:

for i in "$@"; do
    echo $i
done