给定以下数组:
但它返回以下内容:
arr=(hello asd asd1 asd22 asd333)
我想通过其值删除特定项,例如asd
。我做了这个:
IFS=' '
echo "${arr[@]/asd/}"
但它返回以下内容:
hello 1 22 333
所以我写了这个函数:function remove_item() {
local item_search="$1"
shift
local arr_tmp=("${@}")
if [ ${#arr_tmp[@]} -eq 0 ]; then
return
fi
local index=0
for item in ${arr_tmp[@]}; do
if [ "$item" = "$item_search" ]; then
unset arr_tmp[$index]
break
fi
let index++
done
echo "${arr_tmp[*]}"
}
arr=(asd asd1 asd22 asd333)
remove_item 'asd' "${arr[@]}"
输出所需内容:
hello asd1 asd22 asd333
但是我必须在非常长的数组中使用它,并且我必须多次调用它。它的性能很差。
你有什么更好的替代方案吗?任何提示、技巧或建议都将不胜感激。