Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Idiom for booleans
- X-seq: zsh-users 16206
- From: Micah Elliott <micah@xxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Idiom for booleans
- Date: Fri, 12 Aug 2011 17:24:14 -0700
- 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
- Sender: mde@xxxxxxxxxxxxxxxx
Hi! I'm trying to figure out if the $+ expansion, as in ${+param}, is
worth making use of. What idiom do you use for it? I could see it as a
boolean for testing flags. As in:
% verbosity=high myscript.zsh
...
(( $+verbosity )) && print chatty
chatty
% unset verbosity # uh-oh
% (( $+verbosity )) && print chatty # should this work?
((: command not found
I did see this idiom used in the Zsh Guide
(http://zsh.sourceforge.net/Guide/zshguide03.html as: (( OPTIND > 1 ))
&& shift $(( OPTIND - 1 )) ) -- is that a bug?
I suppose it boils down to understanding (explanation welcome here):
% (( 0 ))
((: command not found
% (( 1 )) # ok
Wrapping in "if" makes it work too, but just feels tedious:
% if (( 0 )); then print i say nothing; fi
----
I could make the boolean work by adding a fall-thru:
% (( $+verbosity )) && print chatty || :
but that feels rotten. Or I could use:
% [[ $+verbosity == 1 ]] && print chatty
but that doesn't seem any better than the more common and simpler:
% [[ -n $verbosity ]] && print chatty
So is there some concise idiomatic way of making use of $+ ? Or is
this last line the better way to make such tests?
--
twitter:@Membean | email:Micah@xxxxxxxxxxx | http://Membean.com
Remember your words with Membean. Three free days of learning! GO SIGN UP.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author