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

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.