Print bash arguments in reverse order

前端 未结 5 2242

I have to write a script, which will take all arguments and print them in reverse.

I\'ve made a solution, but find it very bad. Do you have a smarter idea?

5条回答
  •  鱼传尺愫
    2021-01-07 06:08

    Portably and POSIXly, without arrays and working with spaces and newlines:

    Reverse the positional parameters:

    flag=''; c=1; for a in "$@"; do set -- "$a" ${flag-"$@"}; unset flag; done
    

    Print them:

    printf '<%s>' "$@"; echo
    

提交回复
热议问题