<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: How To Find BASH Shell Array Length ( number of elements )</title> <atom:link href="http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/feed/" rel="self" type="application/rss+xml" /><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/</link> <description>Every answer asks a more beautiful question.</description> <lastBuildDate>Fri, 10 Feb 2012 19:55:56 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Dan</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-66322</link> <dc:creator>Dan</dc:creator> <pubDate>Tue, 03 Jan 2012 22:30:38 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-66322</guid> <description>It&#039;s called sharing knowledge.  If you don&#039;t like it, move along.</description> <content:encoded><![CDATA[<p>It&#8217;s called sharing knowledge.  If you don&#8217;t like it, move along.</p> ]]></content:encoded> </item> <item><title>By: Ferk</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-66317</link> <dc:creator>Ferk</dc:creator> <pubDate>Tue, 03 Jan 2012 20:48:33 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-66317</guid> <description>The way it&#039;s done in the post only works when using bash. This is a more POSIX-compatible way of making it work. You can also change the element-separator character to something different than the space, using the variabe IFS.</description> <content:encoded><![CDATA[<p>The way it&#8217;s done in the post only works when using bash. This is a more POSIX-compatible way of making it work. You can also change the element-separator character to something different than the space, using the variabe IFS.</p> ]]></content:encoded> </item> <item><title>By: Ryan</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-63115</link> <dc:creator>Ryan</dc:creator> <pubDate>Mon, 03 Oct 2011 21:48:20 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-63115</guid> <description>lol @ all the bikeshedding syntax scholars here. Ok, your voice has been heard, move along idiots.</description> <content:encoded><![CDATA[<p>lol @ all the bikeshedding syntax scholars here. Ok, your voice has been heard, move along idiots.</p> ]]></content:encoded> </item> <item><title>By: Edward</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-61314</link> <dc:creator>Edward</dc:creator> <pubDate>Fri, 05 Aug 2011 07:17:39 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-61314</guid> <description>1.Write a Linux shell Script to count the Number of user accounts both normal and special or privileged user user accounts on the system.
2.Write an interactive Linux shell script to test whether a file is (a). Read Permission set. (b). Execution permission set (c). Is a non empty file (d). A regular file (e). Ia a directory.</description> <content:encoded><![CDATA[<p>1.Write a Linux shell Script to count the Number of user accounts both normal and special or privileged user user accounts on the system.<br
/> 2.Write an interactive Linux shell script to test whether a file is (a). Read Permission set. (b). Execution permission set (c). Is a non empty file (d). A regular file (e). Ia a directory.</p> ]]></content:encoded> </item> <item><title>By: Stuart Holme</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-50405</link> <dc:creator>Stuart Holme</dc:creator> <pubDate>Wed, 27 Oct 2010 15:35:03 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-50405</guid> <description>Good points on handling a possibly sparse array, Raymond, but the example is pretty useful if you load the array yourself.  Also note that you don&#039;t need to use a variable to store the array length:
for (( i = 0 ; i &lt; ${#my_array[@]} ; i++ )) do; echo &quot;${my_array[i]}&quot;; done
Note the implementations of BASH I am using don&#039;t require &quot;$i&quot; as the index of the array, they work fine with just &quot;i&quot;.  This only seems to work with single character length index variable names though, like &quot;i&quot;, not with, for example, &quot;index&quot;.  YMMV.
Regards,
Stuart</description> <content:encoded><![CDATA[<p>Good points on handling a possibly sparse array, Raymond, but the example is pretty useful if you load the array yourself.  Also note that you don&#8217;t need to use a variable to store the array length:</p><p>for (( i = 0 ; i &lt; ${#my_array[@]} ; i++ )) do; echo &quot;${my_array[i]}&quot;; done</p><p>Note the implementations of BASH I am using don&#039;t require &quot;$i&quot; as the index of the array, they work fine with just &quot;i&quot;.  This only seems to work with single character length index variable names though, like &quot;i&quot;, not with, for example, &quot;index&quot;.  YMMV.</p><p>Regards,<br
/> Stuart</p> ]]></content:encoded> </item> <item><title>By: Dan</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-47862</link> <dc:creator>Dan</dc:creator> <pubDate>Wed, 16 Jun 2010 19:39:48 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-47862</guid> <description>Here&#039;s a quick flush:
NUM=${#a[@]}; for ((i=0;i&lt;${NUM};i++)); do a[${i}]=&#039;&#039;; done
And a for a quick scan, you can do the following either before or after:
COUNT=0; while [ ${COUNT} -lt ${#a[@]} ]; do echo ${a[${COUNT}]}; COUNT=$((COUNT+1)); done</description> <content:encoded><![CDATA[<p>Here&#8217;s a quick flush:<br
/> NUM=${#a[@]}; for ((i=0;i&lt;${NUM};i++)); do a[${i}]=&#039;&#039;; done</p><p>And a for a quick scan, you can do the following either before or after:<br
/> COUNT=0; while [ ${COUNT} -lt ${#a[@]} ]; do echo ${a[${COUNT}]}; COUNT=$((COUNT+1)); done</p> ]]></content:encoded> </item> <item><title>By: Dan</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-47861</link> <dc:creator>Dan</dc:creator> <pubDate>Wed, 16 Jun 2010 19:29:16 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-47861</guid> <description>Well yes, ${#a[@]} pretty much spits out the the number of elements in the array, like the document here said... notice that it&#039;s plainly used as tlen which is the length of the array?
you could do it a million different ways...
NUM=${a[@]}
for ((i=0;i&lt;${NUM};i++)); do echo ${a[${NUM}]}; done
or:
COUNT=0; while [ ${COUNT} -lt ${#a[@]} ]; do echo ${a[${COUNT}]}; COUNT=$((COUNT+1)); done
We could go on and on, since there&#039;s more than one way to skin a cat.</description> <content:encoded><![CDATA[<p>Well yes, ${#a[@]} pretty much spits out the the number of elements in the array, like the document here said&#8230; notice that it&#8217;s plainly used as tlen which is the length of the array?</p><p>you could do it a million different ways&#8230;</p><p>NUM=${a[@]}<br
/> for ((i=0;i&lt;${NUM};i++)); do echo ${a[${NUM}]}; done</p><p>or:</p><p>COUNT=0; while [ ${COUNT} -lt ${#a[@]} ]; do echo ${a[${COUNT}]}; COUNT=$((COUNT+1)); done</p><p>We could go on and on, since there&#039;s more than one way to skin a cat.</p> ]]></content:encoded> </item> <item><title>By: Dan</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-47857</link> <dc:creator>Dan</dc:creator> <pubDate>Wed, 16 Jun 2010 17:56:32 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-47857</guid> <description>That only works if you have no spaces.</description> <content:encoded><![CDATA[<p>That only works if you have no spaces.</p> ]]></content:encoded> </item> <item><title>By: Raymond</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-40167</link> <dc:creator>Raymond</dc:creator> <pubDate>Sat, 07 Feb 2009 10:11:30 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-40167</guid> <description>Problems,
a[2]=222
echo ${#a[@]}
1
Thus this is the lenght of the array, not the index of the last element.
Try instead:
for i in &quot;${a[@]}&quot;; do echo $i; done
Some further ideas may be found at
http://tldp.org/LDP/abs/html/arrays.html
http://www.linuxjournal.com/content/bash-arrays
Best Regards,
Raymond</description> <content:encoded><![CDATA[<p>Problems,</p><p> a[2]=222<br
/> echo ${#a[@]}<br
/> 1</p><p>Thus this is the lenght of the array, not the index of the last element.</p><p>Try instead:<br
/> for i in &#8220;${a[@]}&#8221;; do echo $i; done</p><p>Some further ideas may be found at<br
/> <a
href="http://tldp.org/LDP/abs/html/arrays.html" rel="nofollow">http://tldp.org/LDP/abs/html/arrays.html</a><br
/> <a
href="http://www.linuxjournal.com/content/bash-arrays" rel="nofollow">http://www.linuxjournal.com/content/bash-arrays</a></p><p>Best Regards,<br
/> Raymond</p> ]]></content:encoded> </item> <item><title>By: budacsik</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-38823</link> <dc:creator>budacsik</dc:creator> <pubDate>Sat, 20 Sep 2008 12:18:39 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-38823</guid> <description>Hi!
I think that this example is bad.
I would do this without a block .
#!/bin/bash
NAMESERVERS=&quot;ns1.nixcraft.net. ns2.nixcraft.net. ns3.nixcraft.net.&quot;
for i in $NAMESERVERS; do
echo &quot;$i&quot;
done</description> <content:encoded><![CDATA[<p>Hi!<br
/> I think that this example is bad.<br
/> I would do this without a block .</p><p>#!/bin/bash</p><p>NAMESERVERS=&#8221;ns1.nixcraft.net. ns2.nixcraft.net. ns3.nixcraft.net.&#8221;<br
/> for i in $NAMESERVERS; do<br
/> echo &#8220;$i&#8221;<br
/> done</p> ]]></content:encoded> </item> <item><title>By: those</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-38510</link> <dc:creator>those</dc:creator> <pubDate>Tue, 05 Aug 2008 20:04:12 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-38510</guid> <description>how to clean the array? ;d</description> <content:encoded><![CDATA[<p>how to clean the array? ;d</p> ]]></content:encoded> </item> <item><title>By: Diya</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-38409</link> <dc:creator>Diya</dc:creator> <pubDate>Mon, 28 Jul 2008 08:20:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-38409</guid> <description>Excellent info.</description> <content:encoded><![CDATA[<p>Excellent info.</p> ]]></content:encoded> </item> <item><title>By: S. Nilesh</title><link>http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/#comment-38406</link> <dc:creator>S. Nilesh</dc:creator> <pubDate>Sun, 27 Jul 2008 16:47:29 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1214#comment-38406</guid> <description>Nice one ;)</description> <content:encoded><![CDATA[<p>Nice one ;)</p> ]]></content:encoded> </item> </channel> </rss>
