This little setting drove me crazy:
shopt -s nullglob
I use it in my bash 4.3 script as a global setting to avoid errors scanning empty directories.
Now I found a strange problem which I do not understand:
# shopt -s nullglob
# array=(foo bar)
# echo "${array[@]}"
foo bar
# array=(foo bar?)
# echo "${array[@]}"
foo
# shopt -u nullglob
# array=(foo bar)
# echo "${array[@]}"
foo bar
# array=(foo bar?)
# echo "${array[@]}"
foo bar?
As you can see it removes the bar?
from the array element, but why? The documentation refers to filename patterns which could include *
and ?
, but an array alone has nothing to do with "matching no files":
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
Is this a bug?
P.S. I will set/unset nullglob around loops to solve my problem. This is not the question!
Update1
As @MichaelHomer asked for:
# shopt -s nullglob
# array=(*)
# echo "${array[@]}"
1 Video_folder Server bin config dev etc etc.defaults initrd lib lib32 lib64 lost+found mnt proc root run sbin storage sys tmp tmpRoot usr var var.defaults volume1
# shopt -u nullglob
# array=(*)
# echo "${array[@]}"
1 Video_folder Server bin config dev etc etc.defaults initrd lib lib32 lib64 lost+found mnt proc root run sbin storage sys tmp tmpRoot usr var var.defaults volume1
And this means?
array=(*)
and see what happens. – Michael Homer Aug 10 '19 at 09:48