Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Catch `setopt nounset`
- X-seq: zsh-users 20116
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Catch `setopt nounset`
- Date: Thu, 9 Apr 2015 11:42:05 -0700
- In-reply-to: <mg69er$i52$1@ger.gmane.org>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <mg69er$i52$1@ger.gmane.org>
On Apr 9, 6:30pm, Thorsten Kampe wrote:
}
} I'd to catch the call of a parameter that does not exist with `setopt
} nounset`.
setopt nounset
{
print "$DOESNOTEXIST"
} always {
if (( TRY_BLOCK_ERROR ))
then print "error"
else print "no error"
fi
}
} If I replace the `catch` statement with `if result=$(testfunc)` it
} prints "error". What am I missing here? Why does the latter work but
} not the former?
"parameter not set" is a fatal error, so whatever the current shell is
doing is stopped when that happens. $(testfunc) is run in a subshell,
so the current shell proceeds even though the subshell died.
You could get the same result with
if ( testfunc )
then
printf "no error\n"
else
printf "error\n"
fi
but of course that also suppresses all other side-effects of "testfunc",
which the "always" construct will not.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author