Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:15:15+00:00 2026-06-15T12:15:15+00:00

I’ve got almost the same question as here . I have an array which

  • 0

I’ve got almost the same question as here.

I have an array which contains aa ab aa ac aa ad, etc.
Now I want to select all unique elements from this array.
Thought, this would be simple with sort | uniq or with sort -u as they mentioned in that other question, but nothing changed in the array…
The code is:

echo `echo "${ids[@]}" | sort | uniq`

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:15:16+00:00Added an answer on June 15, 2026 at 12:15 pm

    A bit hacky, but this should do it:

    echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '
    

    To save the sorted unique results back into an array, do Array assignment:

    sorted_unique_ids=($(echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
    

    If your shell supports herestrings (bash should), you can spare an echo process by altering it to:

    tr ' ' '\n' <<< "${ids[@]}" | sort -u | tr '\n' ' '
    

    A note as of Aug 28 2021:

    According to ShellCheck wiki 2207 a read -a pipe should be used to avoid splitting.
    Thus, in bash the command would be:

    IFS=" " read -r -a ids <<< "$(echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"

    or

    IFS=" " read -r -a ids <<< "$(tr ' ' '\n' <<< "${ids[@]}" | sort -u | tr '\n' ' ')"

    Input:

    ids=(aa ab aa ac aa ad)
    

    Output:

    aa ab ac ad
    

    Explanation:

    • "${ids[@]}" – Syntax for working with shell arrays, whether used as part of echo or a herestring. The @ part means "all elements in the array"
    • tr ' ' '\n' – 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.
    • sort -u – sort and retain only unique elements
    • tr '\n' ' ' – convert the newlines we added in earlier back to spaces.
    • $(...) – Command Substitution
    • Aside: tr ' ' '\n' <<< "${ids[@]}" is a more efficient way of doing: echo "${ids[@]}" | tr ' ' '\n'
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.