Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: unshift



On Mon, Apr 1, 2024 at 9:40 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> I've never used shift for anything other than the positionals which is all I'm considering, but if there was an unshift it would obviously have to be globally useful

"Undo the most recent shift" is not how "unshift" is defined in other
languages, e.g., perldoc: "shift" and "unshift" do the same thing to
the left end of an array that "pop" and "push" do to the right end.

If for some reason you don't want to declare a temporary for the old
value of $1, you can also do something like this:

set -- "${(@)argv[2,-1]}" "${argv[1]}"
...
set -- "${argv[-1]}" "${(@)argv[1,-2]}"

That is, rotate the positionals and then rotate them back again.  I
would actually usually do something like

set -- "${(@)argv[2,-1]}" -- "${argv[1]}"
while [[ $1 != -- ]]; do ...; shift; done

but what can be used as a such flag value is context-dependent.




Messages sorted by: Reverse Date, Date, Thread, Author