How to reverse a list of words in a shell string?

MOHAMED picture MOHAMED · Dec 30, 2014 · Viewed 35.2k times · Source

I have a list of words in a string:

str="SaaaaE SeeeeE SbbbbE SffffE SccccE"

I want to reverse it in order to get

"SccccE SffffE SbbbbE SeeeeE SaaaaE"

How I can do that with ash?

Answer

Feten besbes picture Feten besbes · Dec 30, 2014

You can use awk as follows:

echo "$str" | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }'