One approach is to replace the spaces inside the arguments with something else, and then replace the spaces again when using the argument:
dirs="${@/ /###}" for dir in $dirs; do echo "${dir/###/ }" done
It depends on whether you can come up with some sequence of characters that you can be sure will never appear in the real file name.
For a specific situation, when you want to be able to choose between providing an explicit list of directories or the default for the current directory, the best solution is probably to use the function:
do_something() { for dir in " $@ "; do echo "$dir" done } if [ $# -eq 0 ]; then do_something . else do_something " $@ " fi
Or perhaps:
do_something() { echo "$1" } if [ $# -eq 0 ]; then do_something . else for dir in " $@ "; do do_something "$dir" done fi
Ross smith
source share