There are a few ways you can do this, depending on what is convenient and possible.
If you can't change the script but it will handle getting no arguments in a graceful manner, then set the
nullglobshell option usingshopt -s nullglobin the shell which performs the filename globbing operation and calls the script. This will cause the pattern to be removed completely if it does not match. (Note that the pattern will not be replaced by an empty string, as an empty string still counts as one argument.)If you can't change the script and if it can't handle getting no arguments gracefully, then set the
failglobshell option usingshopt -s failglobin the shell which performs the filename globbing operation and calls the script. This will cause the shell to emit an error if the pattern does not match, and the script will not be called at all. If the calling shell is non-interactive (a script), it will terminate if the pattern does not match.If you can change the script, then make it test its first argument with
[ -e "$1" ]to see whether it exists in the filesystem. If it exists, you know that the filename globbing pattern has matched something. You may then continue to process as usual. Otherwise, you may assume that the pattern did not match anything (or that the script was called with no arguments, or that the pattern matched a broken symbolic link (and possibly other things)) and take the necessary actions for this scenario.#!/bin/bash if ! [ -e "$1" ]; then # no files given exit 1 fi # continue processing names from "$@"...Similarly to the previous point, but doing the test before calling the script:
set -- *.txt [ -e "$1" ] && ./script "$@"