Skip to main content
added 1 character in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces, tabs or tab newlines (assuming the default value of $IFS) or containscontain wildcard characters (unless globbing is disabled beforehand) and removes empty elements, and requires the GNU tac command (using tail -r is slightly more portable outside of GNU systems but with some implementations fails on large input).

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain whitespaces or newlines or wildcards or are possibly empty?

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or tab newlines (assuming the default value of $IFS) or contains wildcard characters (unless globbing is disabled beforehand) and removes empty elements, and requires the GNU tac command (using tail -r is slightly more portable outside of GNU systems but with some implementations fails on large input).

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain whitespaces or newlines or wildcards or are possibly empty?

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces, tabs or newlines (assuming the default value of $IFS) or contain wildcard characters (unless globbing is disabled beforehand) and removes empty elements, and requires the GNU tac command (using tail -r is slightly more portable outside of GNU systems but with some implementations fails on large input).

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain whitespaces or newlines or wildcards or are possibly empty?

added 194 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or tab newlines (assuming the default value of $IFS) or contains wildcard characters (unless globbing is disabled beforehand) and removes empty elements, and requires the GNU tac command (using tail -r is slightly more portable outside of GNU systems but with some implementations fails on large input).

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain spaceswhitespaces or newlines or wildcards or are possibly empty?

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or tab newlines (assuming the default value of $IFS) or contains wildcard characters (unless globbing is disabled beforehand) and removes empty elements.

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain spaces or newlines?

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or tab newlines (assuming the default value of $IFS) or contains wildcard characters (unless globbing is disabled beforehand) and removes empty elements, and requires the GNU tac command (using tail -r is slightly more portable outside of GNU systems but with some implementations fails on large input).

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain whitespaces or newlines or wildcards or are possibly empty?

added 143 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or tab newlines (assuming the default value of $IFS) or contains wildcard characters (unless globbing is disabled beforehand) and removes empty elements.

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain spaces or newlines?

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or newlines.

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain spaces or newlines?

I know that it is possible to reverse "$@" using an array:

arr=( "$@" )

And using this answer, reverse the array.

But that requires a shell that has arrays.

It is also possible using tac:

set -- $( printf '%s\n' "$@" | tac )

But that breaks if the parameters have spaces or tab newlines (assuming the default value of $IFS) or contains wildcard characters (unless globbing is disabled beforehand) and removes empty elements.

Is there a way to reverse shell positional arguments portably, without using an array, and that works even if arguments contain spaces or newlines?

Became Hot Network Question
grammar
Source Link
ctrl-alt-delor
  • 28.8k
  • 11
  • 67
  • 114
Loading
Source Link
user232326
user232326
Loading