1

I have some variables in a bash script

variable1=0.111
variable2=-222
variable3=0
variable4=0

....... and so on.

I know i can use sort to sort the variable content, but i want to know which is which after. For example echo $variable1 $variable2 $variable3 $variable4 | xargs -n1 | sort -g would do the tric, but it won't tell me which is which. I need the output to be like : variable2, variable3, variable4 , variable1. Is it possible? Kind regards

vonaka
  • 913
  • 1
  • 11
  • 23

1 Answers1

1

You can do this with sort. Just make sure your echo has these values each on a separate line (great answer on sorting by column here), try this:

echo "variable1 $variable1
variable2 $variable2
variable3 $variable3
variable4 $variable4" | sort -t" " -nk2

From the above linked answer:

-t, - defines your delimiter

-n - gives you numerical sort

-k2 - defines the field (key)

Brandon Miller
  • 4,695
  • 1
  • 20
  • 27