Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A bit hacky, but this should do it:</p> <pre><code>echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ' </code></pre> <p>To save the sorted unique results back into an array, do <a href="https://github.com/koalaman/shellcheck/wiki/SC2178">Array assignment</a>:</p> <pre><code>sorted_unique_ids=($(echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) </code></pre> <p>If your shell supports <a href="http://linux.die.net/abs-guide/x15683.html">herestrings</a> (<code>bash</code> should), you can spare an <code>echo</code> process by altering it to:</p> <pre><code>tr ' ' '\n' &lt;&lt;&lt; "${ids[@]}" | sort -u | tr '\n' ' ' </code></pre> <p><strong>Input:</strong></p> <pre><code>ids=(aa ab aa ac aa ad) </code></pre> <p><strong>Output:</strong></p> <pre><code>aa ab ac ad </code></pre> <p><strong>Explanation:</strong></p> <ul> <li><code>"${ids[@]}"</code> - Syntax for working with shell arrays, whether used as part of <code>echo</code> or a herestring. The <code>@</code> part means "all elements in the array"</li> <li><code>tr ' ' '\n'</code> - Convert all spaces to newlines. Because your array is seen by shell as elements on a single line, separated by spaces; and because sort expects input to be on separate lines.</li> <li><code>sort -u</code> - sort and retain only unique elements</li> <li><code>tr '\n' ' '</code> - convert the newlines we added in earlier back to spaces.</li> <li><code>$(...)</code> - <a href="http://tldp.org/LDP/abs/html/commandsub.html">Command Subsitution</a></li> <li>Aside: <code>tr ' ' '\n' &lt;&lt;&lt; "${ids[@]}"</code> is a more efficient way of doing: <code>echo "${ids[@]}" | tr ' ' '\n'</code></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload