Last active
March 7, 2022 05:27
-
-
Save Integralist/eb7bf0d8f3b7d9958f13 to your computer and use it in GitHub Desktop.
Zsh and Bash Array Shift (remove first item from the Array)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array=(foo, bar, baz) | |
echo ${array[@]} # => foo, bar, baz | |
array=("${array[@]:1}") | |
echo ${array[@]} # => bar, baz | |
array=("${array[@]:1}") | |
echo ${array[@]} # => baz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array=(foo, bar, baz) | |
echo $array # => foo, bar, baz | |
array=("${(@)array:1}") | |
echo $array # => bar, baz | |
array=("${(@)array:1}") | |
echo $array # => baz | |
# UPDATE: this works as well and is less confusing syntax | |
array=(${array:1}) |
For zsh (might work in bash too) (might require some options)
array=($array[2,-1])
or destructively
shift array
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be aware that you'll get the trailing
,
when extracting an item from the Array.To work around that, you need to first get the index of the
,
and then substring it out...You can also avoid ALL of this if you don't make it a comma separated list