Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Want to replace bash w zsh as system shell on Ubuntu



Joke de Buhr wrote:
> On Tuesday 02 February 2010 18:51:28 Frank Terbeck wrote:
[...]
>> > matches=("$HOME"/((.*|*)/)#*(#q^u${SUDO_UID}u${UID},^g${SUDO_GID}g${GID})
>> >) [[ -n ${matches} ]] && chown $SUDO_UID:$SUDO_GID "${matches[@]}"
>> 
>> (( ${#matches} > 0 )) && ...
>> 
>
> [[ -n ${matches} ]] only needs to check if the array "matches" is not empty
> (( ${#matches} > 0 )) needs to calculate the length of the array and compare 
> if the result is > 0. I think [[ -n ${matches} ]] will be more efficient.

I wouldn't bet on that.

I'm not sure how arrays are implemented internally. But I could imagine
the number of elements is kept somewhere and therefore is available
without additional effort (can somebody confirm this?).

Also, I'm not sure what happens exactly, when you do [[ -n $array ]].

Say, $array is ( foo bar baz ). If you now do [[ -n $array ]], what
happens?

a) Does it get expanded to [[ -n "foo bar baz" ]]?
b) Does it get expanded to [[ -n "foo" "bar" "baz" ]]?

If a), zsh would have to create a string from the array in which case
looking at the number of elements is certainly quicker.

If b), what happens if the first entry of the array is an empty string?

Maybe it's even neither of the two and zsh does something smart when an
array is used in a [[ ... ]] style test.

Since I really don't know what it does, I just try:

[snip]
zsh% foo=( foo bar baz )
zsh% set -x; [[ -n $foo ]]; set +x
+/bin/zsh:234> [[ -n 'foo bar baz' ]]
+/bin/zsh:234> set +x
[snap]

Seems like it concatenates the elements into a string. So I think my
  (( ${#array} > 0 ))
approach would win in a benchmark. Unless I'm missing some smart trick
that zsh may use to optimise this.

Regards, Frank



Messages sorted by: Reverse Date, Date, Thread, Author