Loading...

Why can't I pass a directory as an argument to a for loop in bash?

Answer #1 100 %

If you take "$1", it is the first file/directory, which is possible! You should do it in this way:

for i in "[email protected]"
do
  echo The current file is ${i}
done

If you execute it with:

./simple.sh *

They list you all files of the actual dictionary

"$1" is alphabetical the first file/directory of your current directory, and in the for loop, the value of "i" would be e.g. a1.sh and then they would go out of the for loop! If you do:

DIR=/home//* 

you save the value of all files/directories in DIR!

You’ll also like:


© 2022 CodeForDev.com -