I need to remove an element from an array in bash shell. Generally I'd simply do:
array=("${(@)array:#<element to remove>}")
Unfortunately the element I want to remove is a variable so I can't use the previous command. Down here an example:
array+=(pluto)
array+=(pippo)
delete=(pluto)
array( ${array[@]/$delete} ) -> but clearly doesn't work because of {}
Any idea?
http://wiki.bash-hackers.org/syntax/pe#substring_removal
In order to do a full remove element, you have to do an unset command with an if statement. If you don't care about removing prefixes from other variables or about supporting whitespace in the array, then you can just drop the quotes and forget about for loops.
See example below for a few different ways to clean up an array.
Output
Hope that helps.
In ZSH this is dead easy (note this uses more bash compatible syntax than necessary where possible for ease of understanding):
Results: