Wednesday, December 29, 2021

[SOLVED] How to perform the reverse of `xargs`?

Issue

I have a list of numbers that I want to reverse.

They are already sorted.

35 53 102 342

I want this:

342 102 53 35

So I thought of this:

echo $NUMBERS | ??? | tac | xargs

What's the ???

It should turn a space separated list into a line separated list.

I'd like to avoid having to set IFS.

Maybe I can use bash arrays, but I was hoping there's a command whose purpose in life is to do the opposite of xargs (maybe xargs is more than a one trick pony as well!!)


Solution

You can use printf for that. For example:

$ printf "%s\n" 35 53 102 342
35
53
102
342
$ printf "%s\n" 35 53 102 342|tac
342
102
53
35


Answered By - Mat