help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Which is faster `[[ -v x[k] ]]` vs `[[ ${x[k]} ]]`?


From: Stephane Chazelas
Subject: Re: Which is faster `[[ -v x[k] ]]` vs `[[ ${x[k]} ]]`?
Date: Mon, 28 Oct 2019 07:28:09 +0000
User-agent: NeoMutt/20171215

2019-10-27 18:58:22 -0500, Peng Yu:
[...]
> I am confused about which one is faster `[[ -v x[k] ]]` vs `[[ ${x[k]}
> ]]`. Depending on how long it runs, it seems the runtime comparison
> results are different. Why? Which one is supposed to be faster?
[...]

They're not functionaly equivalent.

[[ -v x[k] ]] tests whether the x sparse array has an element
for key k, [[ ${x[k]} ]] tests whether ${x[k]} expands to a
non-empty string (for which you need the x sparse array to have
an element for key k and that element being non-empty).

$ x[12]= k=12
$ [[ -v x[k] ]] && echo yes
yes
$ [[ ${x[k]} ]] || echo no
no

-- 
Stephane





reply via email to

[Prev in Thread] Current Thread [Next in Thread]