Is there a nice way of checking if an array has an element in bash (better than looping through)?
Alternatively, is there another way to check if a number or string equals any of a set of predefined constants?
|
feedback
|
|
In Bash 4, you can use associative arrays:
To set up the array initially you could also do direct assignments:
or this way:
| |||||||||||
feedback
|
|
There is a way to test if an element of an associative array exists (not set), this is different from empty:
Then use it:
| |||||||
feedback
|
|
It's an old question, but I think what is the simplest solution has not appeared yet:
Outputs:
To see how this work check this. | ||||
|
feedback
|
|
I don't think you can do it properly without looping unless you have very limited data in the array. Here is one simple variant, this would correctly say that
The problem is that there is no easy way to add the anchors (that I can think of) besides looping through the array. Unless you can add them before you put them in the array... | |||
feedback
|