shopt -s nullglob # enable nullglob
#shopt -s dotglob # enable dotglob too if desired
cd /path/wanted
files=( * )
cd -
# do something with "${files[@]}" or "${#files[@]}"
shopt -u nullglob # disable nullglob
#shopt -u dotglob # disable dotglob if enabled
With nullglob disabled (default) the array would contain one element containing the glob pattern * if there are no files in the directory.
With nullglob enabled the array will be empty.
The same applies to dotglob: If disabled, dotfiles must be matched explicitly with a pattern like .[^.]* (the [^.] is used to not match the current directory . and the parent directory ..).
With dotglob enabled, the pattern * also matches files starting with a dot.
Related:
files=( /path/wanted/* )– Nov 23 '19 at 09:04files=( * )works perfectly well without a loop. – Nov 23 '19 at 13:11