Is there a data structure for bash scripts that can be used similar to how something like a java.util.Set would be used? Basically a collection that even if you add a duplicate element to it won't allow you to add two of the same element?
I'm not looking to store anything complicated, just a set of strings.
Also, if it does exist, does it require a particular version of bash or is it just a POSIX compliant thing?
I'm aware that bash does have arrays, and some versions of bash have hashmaps (but not all versions).
Best Answer
If all you care about is a set of strings, you can just use an associative array ("hashmap"):
All you care about is whether there's something associated with the key or not. The value doesn’t matter, we only care that there’s a non-empty string there (so you can "delete" an entry by setting it to an empty string).
This would be analogous to using a
HashMap<String,Object>
to represent a set (which is actually what Java’s HashSet does, in fact).These associative arrays are available in Bash 4 and later, and also in zsh and ksh. They don't work in 3-series Bash versions and earlier, including macOS’s Bash 3.2.
There is no POSIX equivalent. You could simulate the effect using
eval
if your strings are suitably restricted, or have a natural transformation to valid variable names:You could also use a temporary file and
grep
, for example, or even lots of temporary files and the filesystem as a key store.It's also possible (perhaps likely) that using some other tool or language is more suitable than shell script. At a minimum,
awk
is available on all POSIX systems and it does support string-keyed associative arrays.If you really do have complex data-structure needs a conventional general-purpose language may be still more appropriate. Perl and Python are also widely available.