mun*_*ish 62 shell bash array shell-script
unset array[0]删除元素但仍然如果我这样做echo ${array[0]}我得到一个空值而且还有其他方法可以做到这一点但是如果数组的元素包含如下空格
array[0]='james young'
array[1]='mary'
array[2]='randy orton'
Run Code Online (Sandbox Code Playgroud)
但这些也无法完成工作
array=${array[@]:1} #removed the 1st element
Run Code Online (Sandbox Code Playgroud)
现在我希望新数组像
array[0]='mary'
array[1]='randy orton'
Run Code Online (Sandbox Code Playgroud)
分配后空格会引起麻烦,实际数组变得像替换一样。
array=(mary randy orton)
Run Code Online (Sandbox Code Playgroud)
man*_*ork 88
只需在赋值中使用数组语法并引用您的变量:
array=("${array[@]:1}") #removed the 1st element
Run Code Online (Sandbox Code Playgroud)
根据评论中的问题进行编辑。因为$@你可以像这样使用它:
set -- "${@:2}" #removed the 1st parameter
Run Code Online (Sandbox Code Playgroud)