I have a variable called var that stores the list of files inside a directory. I loop through all the files and if I find a directory I output "Directory" to the screen. code below:
var=$(ls dir) for file in $var; do if [ -d $dir/$file ]; then echo "Directory" fi done
I have 2 questions:
1st- I am doing this inside a function and I want the $file variable to stay local so when my function ends I don't get access to it. How can I set that variable local? I cant do
for local file in $var; and I cant declare $file before the loop.
2nd- My loop doesnt work when
$(ls dir) contains a file that has a space inside the name. For example if
$dir has 3 files, file1 file 2 and file3,
$var would only hold file1 and file3 as real files but file 2 would not work because it would separate it into file and 2. How can I fix the problem of the files that have space in their names?