If you run rm -r /var/some/dir/* and the dir is empty an error is returned
How can you supress this error so its not returned?
I use it in a linux bash script
Since you question mentions [if] dir is empty an error is returned (so assuming the directory exists), that's because the glob /var/some/dir/* matches nothing, hence bash (with the default unset nullglob and failglob) will pass the string /var/some/dir/* verbatim. And there are no such files, so rm complains.
Since you're using this in a bash script, I would advise you to proceed as follows:
shopt -s nullglob
shopt -u failglob
declare -a files=( /var/some/dir/* )
if (( ${#files[@]} )); then
rm -r -- "${files[@]}" # || error handling
fi
shopt -s nullglob will make globs expand to nothing if no matching files exist,files (and because nullglob is set, the array files is empty if there are no matches),rm them.Proceeding like this will make your script more robust, as you'll be able to catch a genuine error occuring in rm (with the || error handling part) and not confuse this with an error occurring from a non-matched glob.
Doing so, you will not launch a random command with uncontrolled arguments.
Remark. It is not quite true that the error you obtain occurs only when the directory is empty. It occurs when the directory contains no files, except possibly hidden files. If you want to really purge the directory from also potential hidden files, you must shopt -s dotglob so that the glob will also match hidden files.