39

Python has a "set" type which contains unique objects. Does Bash have something equivalent?

I want to keep adding elements to such a bash "set" and never have duplicates.

2
  • 2
    I don't think bash provide such a data structure. In the past, I stored my strings into a file, then sort | uniq
    – Hai Vu
    Aug 17, 2011 at 21:46
  • 2
    you can build such a creature as functions (if_in_set(item,arr), add_setItem(item,arr), (del_setItem(item,arr) ), but you're basically doing a for loop on an array, and if you find the key already used, then you reject the add, else you add the new item. Depending on your needs, this is OK. If your need is a one time, and or urgent, then mySet="$(sort -u tmpFile)" maybe good enough. Good luck.
    – shellter
    Aug 17, 2011 at 21:55

3 Answers 3

21

Bash 4.0 has associative arrays, which can be used to build sets.

Here's an article that discusses them (it was the first Google hit for "bash associative arrays").

(Personally, I'd just use something other than bash, probably Perl.)

3
  • 1
    Ruby is awesome for scripting; My personal favorite Jul 17, 2016 at 15:48
  • 1
    How can you build sets with associative arrays? Jan 22, 2019 at 10:21
  • @HelloGoodbye Use the members of the set as the keys, and some arbitrary value as the corresponding value. Jan 22, 2019 at 20:57
16
some_dups=(aa aa b b c)
uniques=($(for v in "${some_dups[@]}"; do echo "$v";done| sort| uniq| xargs))
echo "${uniques[@]}"

gives

aa b c

also in bash 3, where no associative arrays are available

1
  • 3
    Thanks! You can also simplify: uniques=($(for v in "${some_dups[@]}"; do echo "$v";done| sort -u))
    – Marboni
    Aug 5, 2016 at 13:46
13

After some googling I found a nice bash implementation at http://www.catonmat.net/blog/set-operations-in-unix-shell-simplified/. It has all the usual set operators in multiple ways, and even a printable pdf cheatsheet.

I've used associative arrays though, it's much more readable.

Declare a setA associative array variable:

$ declare -A setA

Or declare and add the initial members at the same time:

$ declare -A setA=([memberA]=1 [memberB]=1)

Add a member to the set:

$ setA[memberC]=1

Test for membership:

$ [ -n "${setA[memberC]}" ] && echo in set || echo not in set
in set

$ [ -n "${setA[memberD]}" ] && echo in set || echo not in set
not in set

List members (space separated):

$ echo "${!setA[@]}"
memberA memberC memberB

or (newline separated):

$ printf '%s\n' "${!setA[@]}"
memberB
memberC
memberA

Iterate through members:

$ for m in "${!setA[@]}"; do echo "$m"; done
memberB
memberC
memberA

Cardinality (number of members in the set):

$ echo ${#setA[@]}
3

Remove a member:

$ unset setA[memberC]

Use quotes to add members with spaces in their name:

$ setA["member with space"]=1

And it's possible to use variables as members too:

$ read -r str
$ setA["$str"]=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.