I have a problem when looping over such a variable. I have prepared 2 examples to show the problem.
ex1:
#!/bin/bash
DIRS="$@"
for DIR in $DIRS; do
echo "$DIR"
done
ex2:
#!/bin/bash
for DIR in "$@"; do
echo "$DIR"
done
The second example works as expected (and required). A quick test follows:
$ ex1 "a b" "c"
a
b
c
$ ex2 "a b" "c"
a b
c
The reason, why I want to use the first method is because I want to be able to pass multiple directories to the program or none to use the current dir. Like so:
[ $# -eq 0 ] && DIRS=`pwd` || DIRS="$@"
So, how do I get example 1 to be space-safe?