Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting List and adding sorted values to new list in bash

I am trying to sort all values of a list and add them, sorted, into another list . The sort -nu works fine but he doesn't want to add $i to the new SortedList. What could be the problem here?

function Sort() {
   SortedList=""
   for i in $list;
   do
      echo $i
      $SortedList= "$SortedList $i"
   done | sort -nu
}

echo " This is the sorted list : $SortedList"

like image 437
Mathias Verhoeven Avatar asked Sep 01 '25 10:09

Mathias Verhoeven


1 Answers

I think you can do something like the following.

#!/bin/bash
list="7 4 2 5 3"

function Sort() {
    SortedList=$(echo $list | tr " " "\n" | sort -nu)
}
Sort
echo $SortedList

It may fail because we still don't know how your list looks like.

like image 190
Rambo Ramon Avatar answered Sep 03 '25 16:09

Rambo Ramon