Search code examples
bashshellterminalglobgraceful-degradation

graceful degradation for globbing on terminal


Whenever glob pattern match fails, it stops the whole job. For instance,

$ mv *.jpg *.png folder1 && blahblah
mv: cannot stat `*.jpg': No such file or directory

*.png isn't moved to folder1 and blahblah is not run.

And the script below works only for the case when both .[A-z]* and * succeed.

#!/bin/bash
cd $1
du -sk .[A-z]* *| sort -rn | head

How do I make globbing fail gracefully, at most only displaying warnings, but never stopping the job?


Solution

  • In Bash, shopt -s nullglob will allow a failed glob to expand to nothing with no errors.