stringbashshelluniquedistinct-values

How to get string with disctinct (unique) characters from existing string in bash script?


For example, I have string:

explicit_borders="\"\"''()"

How to calculate based on this string the string with the following result:

distinct_border_chars="\"'()"  # contains only unique set of chars

To solve this should I loop over each char in the string and add the char only if the result string does not contain it? Or perhaps is there some simpler bash-specific solution?


Update

I would prefer solutions based on bash built-ins without additional subshell creation (that are using pipes | and command substitution with $()) since they will work better from stand point of performance. And better without here-strings (<<< ...) since they are creating temporary files which impacts performance.

But actually any solutions can also be considered (especially simple and concise) to have more choice.


Solution

  • Yes it can be done in pure bash, all we need is an associative array to behave as a hash-map and key each character as it occurs in the input string. If the char has appeared once, append it to the final result

    #!/usr/bin/env bash
    
    explicit_borders="\"\"''()"
    declare -A seen
    result=""
    
    for (( i=0; i<${#explicit_borders}; i++ )); do
        c="${explicit_borders:$i:1}"
        if ! [[ -n "${seen[$c]}" ]]; then
            seen[$c]=1
            result+="$c"
        fi
    done
    
    printf -v result '%q' "$result"
    printf "%s\n" "${result}"
    

    The %q format specifier at the end causes printf to output the corresponding argument in a format that can be reused as shell input.