assert

SYNOPSIS

assert command arg ...

DESCRIPTION

assert evals the command and if the exit/return status is non-zero calls die with the message FILE NAME: LINENO: FUNCNAME: Assertion 'command'(args) failed. arg is not part of the command which is eval'd but may be useful in some cases as given below.

Please note that assert is a function which calls eval so if you pass a positional parameter unexpanded it will be expanded as the positional parameter of assert and not of the caller.So:

assert '[[ -f $1 ]]'

will expand to:

assert '[[ -f ]]' #since $1 is not set

As a workaround you can expand $1 while calling assert and let eval act on expanded result.This is dangerous if you are not sure what $1 contains:

assert "[[ -f $1 ]]" #dangerous, what if $1 was "file ]] ; rm -rf /"

Or you can make use of positional parameter to assert:

assert '[[ -f $2 ]]' "$1" #safe