If you want to work with an arrayed expansion result in a basic POSIX shell you have to take care with both $IFS
and filename generation. You can work safely with unquoted variables - but it is usually not the most simple way to do it.
Shell's are designed to work first and foremost with arguments - so I suggest you use the argument array:
set -- "${FILES%/*}"/*
You'll have to check a glob's result though - in any POSIX shell if the glob doesn't resolve it will still remain an iterable parameter. This is true of for var in glob*
as well - without testing the results you cannot be sure that you're actually working with a file. So...
for f do [ -e "$f" ] || [ -L "$f" ] && printf %s\\n "$f"; done
You can iterate over the items in your arg array like for arg do ...;done
. You can also address them from within the loop as "$1"
"$2
" and etc, or as a whole with either of "$@"
and "$*"
depending. The array remains set when the loop is complete.
One last note though, as I mentioned before, filename generation is a configurable feature of the shell. It can be disabled w/ set -f
or enabled w/ set +f
. What's more, it only works if there are matching filenames in the directory in which you search - if there are none, you just get your unresolved glob back. Globbing also requires both search and execute permissions for a directory. So, if you find you cannot resolve a glob match, you might try enabling filename generation, checking the path's permissions, and verifying there are files there in the first place.
FILES=(path/to/files/*); for f in "${FILES[@]}"; do echo "$f"; done
– jasonwryan Feb 24 '15 at 17:22Syntax error: "(" unexpected
. I understand that you're creating an array containing the /path/to/files, but this isn't working. – Stephen Feb 24 '15 at 17:56