<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Tech-Recipes - Latest Comments in bash array operations</title><link>http://tech-recipes.disqus.com/</link><description>Cookbook of Tech Tutorials</description><atom:link href="https://tech-recipes.disqus.com/bash_array_operations_bourne_shell_scripting_tech_recipes/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 01 Oct 2009 22:13:46 -0000</lastBuildDate><item><title>Re: bash array operations</title><link>http://www.tech-recipes.com/rx/910/bash-array-operations/#comment-18117581</link><description>&lt;p&gt;and...here's the real solution...&lt;/p&gt;&lt;p&gt;function remel() {&lt;br&gt;    array=$1&lt;br&gt;    i=$2&lt;br&gt;    if [ $i -ne 0 ]; then&lt;br&gt;        eval "$array"="( `eval echo \$\{$array[@]:0:$i\} \$\{$array[@]:$(($i + 1))\}` )";&lt;br&gt;    else&lt;br&gt;        eval "$array"="( `eval echo \$\{$array[@]:$(($i + 1))\}` )";&lt;br&gt;    fi&lt;br&gt;}&lt;/p&gt;&lt;p&gt;#to use it:&lt;br&gt;arr=( a b c d )&lt;br&gt;echo ${arr[@]}&lt;br&gt;echo ${arr[1]}&lt;br&gt;#now it actually just removes the element...&lt;br&gt;remel arr 1&lt;br&gt;echo ${arr[@]}&lt;br&gt;echo ${arr[1]}&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DemonWeasel</dc:creator><pubDate>Thu, 01 Oct 2009 22:13:46 -0000</pubDate></item><item><title>Re: bash array operations</title><link>http://www.tech-recipes.com/rx/910/bash-array-operations/#comment-18086214</link><description>&lt;p&gt;or if you want a more standard syntax of taking an array in as a parameter&lt;/p&gt;&lt;p&gt;function remel(){&lt;br&gt;    array=( $@ )&lt;br&gt;    let ind=${#array[@]}-1&lt;br&gt;    i=${array[$ind]}&lt;br&gt;    unset array[$ind]&lt;br&gt;    if [ $i -ne 0 ]; then&lt;br&gt;        array=( ${array[@]:0:$i} ${array[@]:$(($i + 1))} )&lt;br&gt;    else&lt;br&gt;        array=( ${array[@]:$(($i + 1))} )&lt;br&gt;    fi&lt;br&gt;    echo ${array[@]}&lt;br&gt;}&lt;/p&gt;&lt;p&gt;#to use it:&lt;br&gt;arr=( a b c d )&lt;br&gt;echo ${arr[@]}&lt;br&gt;echo ${arr[1]}&lt;br&gt;#to remove element 1 set the array equal to an array of the output&lt;br&gt;arr=( `remel ${arr[@]} 1` )&lt;br&gt;echo ${arr[@]}&lt;br&gt;echo ${arr[1]}&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DemonWeasel</dc:creator><pubDate>Thu, 01 Oct 2009 21:32:33 -0000</pubDate></item><item><title>Re: bash array operations</title><link>http://www.tech-recipes.com/rx/910/bash-array-operations/#comment-18067848</link><description>&lt;p&gt;#a function to create a new array minus the specified element&lt;/p&gt;&lt;p&gt;function remel() {&lt;br&gt;    array=( `eval echo '${'$1"[@]"'}'` )&lt;br&gt;    i=$2&lt;br&gt;    #remove EL from array&lt;br&gt;    if [ $i -ne 0 ]; then&lt;br&gt;        array=( ${array[@]:0:$i} ${array[@]:$(($i + 1))} )&lt;br&gt;    else&lt;br&gt;        array=( ${array[@]:$(($i + 1))} )&lt;br&gt;    fi&lt;br&gt;    echo ${array[@]}&lt;br&gt;}&lt;/p&gt;&lt;p&gt;#to use it:&lt;br&gt;arr=( a b c d )&lt;br&gt;echo ${arr[@]}&lt;br&gt;echo ${arr[1]}&lt;br&gt;#to remove element 1 set the array equal to an array of the output&lt;br&gt;arr=( `remel arr 1` )&lt;br&gt;echo ${arr[@]}&lt;br&gt;echo ${arr[1]}&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DemonWeasel</dc:creator><pubDate>Thu, 01 Oct 2009 20:48:07 -0000</pubDate></item><item><title>Re: bash array operations</title><link>http://www.tech-recipes.com/rx/910/bash-array-operations/#comment-15295017</link><description>&lt;p&gt;unset command is enough&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">cori_chenxx</dc:creator><pubDate>Sun, 23 Aug 2009 21:25:03 -0000</pubDate></item><item><title>Re: bash array operations</title><link>http://www.tech-recipes.com/rx/910/bash-array-operations/#comment-2769517</link><description>&lt;p&gt;&lt;code&gt;#remove i from array&lt;br&gt;unset array[$i]&lt;br&gt;array=( ${array[@]} )&lt;/code&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Fri, 29 Aug 2008 02:26:47 -0000</pubDate></item><item><title>Re: bash array operations</title><link>http://www.tech-recipes.com/rx/910/bash-array-operations/#comment-2769516</link><description>&lt;p&gt;so looking at the tip found here:&lt;br&gt;&lt;a href="http://www.tech-recipes.com/bourne_shell_scripting_tips910.html" rel="nofollow noopener" target="_blank" title="http://www.tech-recipes.com/bourne_shell_scripting_tips910.html"&gt;http://www.tech-recipes.com...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;:oops: works as long as you do not try and remove element 0. Look at just this part:&lt;br&gt;&lt;code&gt;${array[@]:0:$i}&lt;/code&gt;&lt;br&gt; and you will notice the array is rebuilt using items 0 through $i. The 0 will cause element 0 to stay in the array even if &lt;code&gt; i=0&lt;/code&gt;&lt;br&gt; Does anyone have a better fix than this:&lt;br&gt;&lt;code&gt;#remove i from array&lt;br&gt;if [ $i -ne 0 ]; then&lt;br&gt;	array=( ${array[@]:0:$i} ${array[@]:$(($i + 1))} )&lt;br&gt;else&lt;br&gt;	array=( ${array[@]:$(($i + 1))} )&lt;br&gt;fi&lt;br&gt;&lt;/code&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Sun, 25 Mar 2007 11:41:06 -0000</pubDate></item></channel></rss>