I have a bash script that lists all files in a directory:for file in foo/*; do  echo ${file##*/}doneIt works fine if there are files in the directory, but if there are no files it lists files one directory up.How can I fix this?
Command lists wrong
bash;for
If there is nothing in the foo directory, the variable file will get the literal value foo/*.  The substitution ${file##*/} will yield the string *, and with echo that will output all names in the current directory.To fix this in bash, the simplest option would be to set the nullglob shell option with shopt -s nullglob. This ensures that file name globbing doesn't expand to the pattern itself if there are no names matching the pattern.From the bash manual:nullglobIf set, bash allows patterns  which  match  no  files                        (see  Pathname  Expansion  above) to expand to a null                        string, rather than themselves.Ps. I wish that the manual used names or possibly filenames rather than files.