从数组壳中取出元素 [英] Remove element from array shell

查看:108
本文介绍了从数组壳中取出元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在bash shell中的数组中删除元素。
一般来说,我会简单地做:

I need to remove an element from an array in bash shell. Generally I'd simply do:

array=("${(@)array:#<element to remove>}")

不幸的是我要删除的元素是一个变量,所以我不能用previous命令。
到这里一个例子:

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 {}

任何想法?

推荐答案

,你想在下面的作品庆典的zsh

The following works as you would like in bash and zsh:

$ array=(pluto pippo)
$ delete=(pluto)
$ echo ${array[@]/$delete}
pippo
$ array=( "${array[@]/$delete}" )

如果需要删除多个元素:

If need to delete more than one element:

...
$ delete=(pluto pippo)
for del in ${delete[@]}
do
   array=(${array[@]/$del})
done

买者

该技术也会删除prefixes匹配 $删除从元素,不一定所有元素。

This technique actually removes prefixes matching $delete from the elements, not necessarily whole elements.

这篇关于从数组壳中取出元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆