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.
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.
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.
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]
$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.
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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With