With recent versions of bash
(4.3 or above), you can do:
unset 'array[-1]'
to unset the element with highest indice, like in zsh
:
$ bash -c 'a[3]=1 a[12]=2; a[123123]=3; typeset -p a; unset "a[-1]"; typeset -p a' declare -a a=([3]="1" [12]="2" [123123]="3") declare -a a=([3]="1" [12]="2")
That also works in ksh93 since ksh93t.
Note that the quotes are necessary as [...]
is a glob operator in Bourne-like shells. If there was a file called array1
in the current directory for instance, an unquoted array[-1]
would expand to array1
, and if there wasn't that would either expand to nothing or to array[-1]
or cause an error depending on the shell and glob option settings.
In zsh
(where arrays are normal arrays, not those sparse arrays of ksh/bash), beside unset 'array[-1]'
, you can also do:
array[-1]=()
(same for unsetting any element and shift the ones after it, while unset
would set an element to the empty string when it's not the last to keep some level of compatibility with ksh).
In yash
(also with normal arrays):
array -d array -1
In fish
(also with normal arrays):
set -e array[-1]
In csh
(also with normal arrays, and the first shell with array support (since the late 70s!)):
set array[$#array] =
find
: Make 'find -regextype egrep' as alias Also my answer to another question: How to use find command to list file names but not path?isufx:
disappear to when you echo it? Consider making sure that the code that you show corresponds to the output that you show, or we can never be certain about what code you are actually working with.