Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "set" data structure in bash?

Tags:

bash

set

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.

like image 305
McBear Holden Avatar asked Aug 17 '11 21:08

McBear Holden


People also ask

Are there data structures in Bash?

Arrays are one of the most used and fundamental data structures. You can think of an array is a variable that can store multiple variables within it. In this article, we'll cover the Bash arrays, and explain how to use them in your Bash scripts.

What is set +E in Bash?

Set –e is used within the Bash to stop execution instantly as a query exits while having a non-zero status. This function is also used when you need to know the error location in the running code.

WHAT IS set command in Bash?

We use the set command to change the values of shell options and display variables in Bash scripts. We can also use it to debug Bash scripts, export values from shell scripts, terminate programs when they fail, and handle exceptions. The set command has the following format: $ set [option] [argument]

What does [- Z $1 mean in Bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.


2 Answers

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

like image 97
Keith Thompson Avatar answered Oct 19 '22 13:10

Keith Thompson


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

like image 16
vak Avatar answered Oct 19 '22 13:10

vak