Is there a “set” data structure in bash?

2019-02-08 08:03发布

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.

标签: bash set
3条回答
Root(大扎)
2楼-- · 2019-02-08 08:44

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:

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

or:

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

Cardinality (number of members in the set):

$ echo ${#setA[@]}
3

Remove a member:

$ unset setA[memberC]
查看更多
孤傲高冷的网名
3楼-- · 2019-02-08 08:49

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.)

查看更多
何必那么认真
4楼-- · 2019-02-08 08:50
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

查看更多
登录 后发表回答