Remove entry from array

Viewed 13607

I want to do sth. like this:

foo=(a b c)
foo-=b
echo $foo # should output "a c"

How can I remove an entry from an array? foo-=b does not work.

The removal should work no matter where the entry is.

5 Answers

Since versions 4.2 and 5.0, zsh accepts ${name:|arrayname} syntax. From manual:

If arrayname is the name (N.B., not contents) of an array variable, then any elements contained in arrayname are removed from the substitution of name.

So, it does exactly what you expect:

$ foo=(a b c)
$ excl=(b)
$ echo ${foo:|excl}
a c
Related