This method is more efficient than checking for existence at each iteration:
set q-* [ -e "$1" ] || shift for z; do echo "$z" done
We use set to extend the template in the shell argument list. If the first element of the argument list is not a valid file, glob does not match. (Unlike some common attempts, this works correctly, even if the first glob match was in a file whose name matches the glob pattern.)
If there is no match, the argument list contains one element, and we disable it so that the argument list is now empty. Then the for loop will not do any iteration at all.
Otherwise, we iterate over the list of arguments that extended glob (this is an implicit behavior when there is no in elements after for variable ).
source share