bug-bash
[Top][All Lists]
Advanced

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

How to presence-detect an array variable or subscript thereof with `test


From: Tim Friske
Subject: How to presence-detect an array variable or subscript thereof with `test -v`?
Date: Tue, 27 Nov 2012 19:57:28 +0100

Hi folks,

I came accross the `-v` option of the `test` command and wondered how I
would possibly test not only string- and integer- but also array variables
as a whole and in parts.

I thought it should be possible to say:

declare -Ai foobar=([bar]=0)
test -v foobar[foo] && echo true || echo false
# Output: true
test -v foobar[bar] && echo true || echo false
# Output: true

Even quoting doesn't help here except for the opposite results:

test -v "foobar[foo]" && echo true || echo false
# Output: false
test -v "foobar[bar]" && echo true || echo false
# Output: false

Obviously the results should be "false" and "true", respectively.

Apart from arrays everything else works as follows:

test -v a && echo true || echo false
# Output: false
declare a=""
test -v a && echo true || echo false
# Output: true
test -v b && echo true || echo false
# Output: false
declare -i b=0
test -v b && echo true || echo false
# Output: true
test -v c && echo true || echo false
# Output: false
declare -a c=()
test -v c && echo true || echo false
# Output: true

Cheers,
Tim


-- 
--
`~~~~°<
C92A E44E CC19 58E2 FA35 4048 2217 3C6E 0338 83FC


reply via email to

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