Array.Unshift for C# Arrays - Questions & Answers - Unity Discussions
Unshift array element in Bash script - Stack Overflow
In php, why array_unshift() is named unshift? - Stack Overflow
Who thought "unshift()" was a good name? Why?
Videos
If your array is contiguous, you can use the "${array[@]}" syntax to construct a new array:
array=('a' 'b' 'c');
echo "${array[@]}"; # prints: a b c
array=('d' "${array[@]}");
echo "${array[@]}"; # prints: d a b c
As chepner mentions, the above method will collapse indices of sparse arrays:
array=([5]='b' [10]='c');
declare -p array; # prints: declare -a array='([5]="b" [10]="c")'
array=('a' "${array[@]}");
declare -p array; # prints: declare -a array='([0]="a" [1]="b" [2]="c")'
(Fun fact: PHP does that too - but then again, it's PHP :P)
If you need to work with sparse arrays, you can iterate over the indices of the array manually (${!array[@]}) and increase them by one (with $((...+1))):
old=([5]='b' [10]='c');
new=('a');
for i in "${!old[@]}"; do
new["$(($i+1))"]="${old[$i]}";
done;
declare -p new; # prints: declare -a new='([0]="a" [6]="b" [11]="c")'
Non-bash version: POSIX shells don't really have arrays, excepting shell parameters, (i.e. $1, $2, $3, ...),but for those parameters this should work:
set - a b c ; echo $1 $3
Output:
a c
Now add "foo" to the beginning:
set - foo "$@" ; echo $1 $3
Output:
foo b
It's historical. Whereas in newer languages we probably wouldn't call it "shift" any more, older terminology from Perl includes the shift keyword, whose job is to:
[Shift] the first value of the array off and returns it, shortening the array by 1 and moving everything down.
This is often used with the @_ array, which contains function arguments, and shift would provide access to them one at a time.
PHP, in its early days when Perl was still widely used for web programming, has simply taken this terminology, added the "inverse" unshift and left it.
Another example is glob, which is named after the libc glob() function. I imagine the intent was to make these functions familiar to those coming to PHP from existing languages, but in retrospect a decade or so later, perhaps the terms have aged poorly.
I can't find a definitive source for this, but my guess is that it stemmed from a familiarity with shift registers, which behave in a similar manner.
You'll find similar functions in Perl, which PHP borrows a lot from. I would guess Perl also borrowed from other sources too, but ultimately, you'll get to a language designer who felt it was logical to call the operation 'shift' as it was similar to a shift register.
Other languages use the same naming idiom, for example Javascript and ActionScript, but other languages might use other terms, like queue/dequeue - see C#
EDIT: This is just venting, not confusion on how the word is technically used by JS :D
OK, this is why I keep starting and stopping learning JS- reserved words with extremely unrelated English meanings. "Unshift" means a previous shift occurred. That's the only meaning for it in English. But JS decided to use it as a prepend array method. Argh!!
That is all...