Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

unset array[0] removes the element but still if I do echo ${array[0]} I get a null value moreover there are other ways of doing this but if an element of an array contains spaces like below

array[0]='james young'
array[1]='mary'
array[2]='randy orton'

but these also fail to do the job

array=${array[@]:1} #removed the 1st element

now I want the new array to be like

array[0]='mary'
array[1]='randy orton'

The spaces cause the trouble after assignment and the actual array becomes like with substitution.

array=(mary randy orton)
share|improve this question
2  
No, not the spaces cause trouble, but the lack of quoting. –  manatwork Mar 18 '13 at 12:33

2 Answers 2

up vote 12 down vote accepted

Just use array syntax on the assignment and quote your variable:

array=("${array[@]:1}") #removed the 1st element
share|improve this answer
3  
Note that it doesn't remove the 1st element but the element of indice 0 and reassigns the indices. If the first element was on indice 12, it won't remove anything but will reassign the indices so that what once was on indice 12 will now be on indice 0. It's probably not a concern in the OP's case but should probably be noted for future reference. The behaviour is different with zsh whose arrays are not sparse contrary to ksh or bash. –  Stéphane Chazelas Mar 18 '13 at 13:05
    
+1 i guess i am still wrong then if it does not remove the element.I was under the impression that array=("${array[@]:1}") removes the 1st element. –  munish Mar 19 '13 at 6:08
    
Hi @StephaneChazelas. The singular of "indices" is "index". Thanks for your comment! –  Steven Lu Aug 10 '13 at 23:09
#!/bin/bash

q=( one two three four five )

echo -e "
  (remove) { [:range:] } <- [:list:]
                | [:range:] => return list with range removed range is in the form of [:digit:]-[:digit:]
"

function remove {
  if [[ $1 =~ ([[:digit:]])(-([[:digit:]]))?   ]]; then
    from=${BASH_REMATCH[1]}
    to=${BASH_REMATCH[3]}
  else
    echo bad range
  fi;shift
  array=( ${@} )
  local start=${array[@]::${from}}
  local rest
  [ -n "$to" ] && rest=${array[@]:((${to}+1))}  || rest=${array[@]:((${from}+1))}
  echo ${start[@]} ${rest[@]}
}

q=( `remove 1 ${q[*]}` )
echo ${q[@]}
~                                                                                                                                                              
~                       
share|improve this answer
1  
This would be much better if there was something to explain how it works and not just a blob of code. And what's with the tildes at the bottom? –  Michael Kjörling Oct 8 '13 at 20:16
1  
Seriously, you are correct. This does look like it was written by a hooligan, but thank you. I really only get to sneak this in between hamburger serving days. –  James Andino Oct 9 '13 at 6:54
    
If any element of q has spaces in it, this will break it up into multiple elements. –  William Everett Aug 21 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.