Is there an easy way to set nullglob for one glob
With mapfile
in Bash 4, you can load an array from a subshell with something like: mapfile array < <(shopt -s nullglob; for f in ./*; do echo "$f"; done)
. Full example:
$ shopt nullglobnullglob off$ find../bar baz./qux quux$ mapfile array < <(shopt -s nullglob; for f in ./*; do echo "$f"; done)$ shopt nullglobnullglob off$ echo ${#array[@]}2$ echo ${array[0]}bar baz$ echo ${array[1]}qux quux$ rm *$ mapfile array < <(shopt -s nullglob; for f in ./*; do echo "$f"; done)$ echo ${#array[@]}0
- Be sure to glob with
./*
instead of a bare*
when usingecho
to print the file name - Doesn't work with newline characters in the filename :( as pointed out by derobert
If you need to handle newlines in the filename, you will have to do the much more verbose:
array=()while read -r -d $'\0'; do array+=("$REPLY")done < <(shopt -s nullglob; for f in ./*; do printf "$f\0"; done)
But by this point, it may be simpler to follow the advice of one of the other answers.