files as a scalar variable instead of an array variable.
You're assigning some string like
/home/highsciguy/print/*.pdf to the
$files scalar (aka string) variable.
instead. The shell will expand that globbing pattern into a list of file paths, and assign each of them to elements of the
The expansion of the glob is done at the time of the assignment.
You don't have to use non-standard sh features, and you could use your system's
sh instead of
bash here by writing it:
[ "$#" -gt 0 ] || set -- ~/print/*.pdf
for file do
ls -d -- "$file"
set is to assign the
"$@" array of positional parameters.
Another approach could have been to store the globbing pattern in a scalar variable:
And have the shell expand the glob at the time the
$files variable is expanded.
IFS= # disable word splitting
for file in $files; do ...
$files is not quoted (which you shouldn't usually do), its expansion is subject to word splitting (which we've disabled here) and globbing/filename generation.
*.pdf will be expanded to the list of matching files. However, if
$HOME contained wildcard characters, they could be expanded too, which is why it's still preferable to use an array variable.