Gil Kenot's answer works great and is very brief; if you are looking for solutions that express intent, you can try them that are based on parameter counting, $# :
[[ $# -gt 0 ]] || { helptext; exit 1; }
Alternative using arithmetic expressions:
(( $# > 0 )) || { helptext; exit 1; }
Finally, a shorthand that relies on 0 evaluating to false, and any nonzero number is true:
(( $# )) || { helptext; exit 1; }
William Pursell offers another option that is both descriptive and POSIX compatible:
test $# -gt 0 || { helptext; exit 1; }
test / [ ... ] is a POSIX / built-in utility, while a similar condition is [[ ... ]] bash specific (as is (( ... )) ).
However, as a rule, bash [[ ... ]] offers more features and has fewer surprises than test / [...] .
mklement0
source share