{"id":866,"date":"2020-12-05T20:28:15","date_gmt":"2020-12-05T18:28:15","guid":{"rendered":"https:\/\/www.bashpi.org\/?page_id=866"},"modified":"2021-04-13T10:09:38","modified_gmt":"2021-04-13T07:09:38","slug":"bash-arrays","status":"publish","type":"page","link":"https:\/\/www.bashpi.org\/?page_id=866","title":{"rendered":"Bash arrays"},"content":{"rendered":"\n<p>There are two types of arrays you can use &#8211; indexed and associative arrays.<\/p>\n\n\n\n<p><strong>Start by declaring the arrays<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ declare -a indexed_array\n$ declare -A associative_array<\/pre>\n\n\n\n<p><strong>Add values to arrays<\/strong> &#8211; note the possibility to add values to arrays with += operator.<\/p>\n\n\n\n<pre id=\"block-69cf9b09-a11c-4ffc-a49c-95e0918d6d1e\" class=\"wp-block-preformatted\">$ indexed_array[0]=\"value1\"\n$ indexed_array[1]=\"value2\"\n$ indexed_array[2]=\"value3\"\n$ indexed_array+=\"value4\"\n$ indexed_array+=\"value5\"\n\n$ associative_array[one]=\"value1\"\n$ associative_array[two]=\"value2\"\n$ associative_array[three]=\"value3\"\n\n# or add multiple values like this\n\n$ indexed_array=(value6 value7 value8)\n$ indexed_array+=(value4 value5)\n$ associative_array=([one]=value1 [two]=value2 [three]=value3)\n$ associative_array+=([four]=value4 [five]=value5)<\/pre>\n\n\n\n<p><strong>Print out array values<\/strong><\/p>\n\n\n\n<pre id=\"block-69cf9b09-a11c-4ffc-a49c-95e0918d6d1e\" class=\"wp-block-preformatted\">$ echo ${indexed_array[@]}\n\n# or \n\n$ echo ${associative_array[*]}\n<\/pre>\n\n\n\n<p><strong>Print out array indexes or keys<\/strong> &#8211; add ! before array name<\/p>\n\n\n\n<pre id=\"block-4e205e37-3f8c-457b-aced-40bc24ba8997\" class=\"wp-block-preformatted\">for index in \"${!indexed_array[@]}\"; do \n echo \"${index}\"\ndone\n\nfor key in \"${!associative_array[@]}\"; do\necho \"$key\"\ndone<\/pre>\n\n\n\n<p><strong>Number of values in arrays<\/strong> &#8211; use # before array name<\/p>\n\n\n\n<pre id=\"block-f458f2b2-c2e4-402e-af4f-925e5a2e68ef\" class=\"wp-block-preformatted\">echo \"indexed array contains ${#indexed_array[@]} values\"\necho \"associative_array array contains ${#associative_array[@]} values\"<\/pre>\n\n\n\n<p><strong>Deleting values from an array<\/strong> &#8211; use unset<\/p>\n\n\n\n<pre id=\"block-86a5d521-b9df-49ff-95a5-fed57f0ebe54\" class=\"wp-block-preformatted\">unset indexed_array[2]\nunset associative_array[three]<\/pre>\n\n\n\n<p><strong>Deleting entire array<\/strong> &#8211; useful if you play around with lost of data<\/p>\n\n\n\n<pre id=\"block-d24a66c6-2144-4a11-a55c-39d7faaf7452\" class=\"wp-block-preformatted\">unset indexed_array\nunset associative_array<\/pre>\n\n\n\n<p><strong>Read file into array <\/strong>&#8211; you don&#8217;t need to loop trough each line<\/p>\n\n\n\n<p>Use bash internal command readarray.  -t option removes trailing newline. More options look in <code>man bash<\/code>. Following will read each line of file.txt into indexed array named <em>indexed_array<\/em>.<\/p>\n\n\n\n<pre id=\"block-86a5d521-b9df-49ff-95a5-fed57f0ebe54\" class=\"wp-block-preformatted\">readarray -t indexed_array &lt; file.txt<\/pre>\n\n\n\n<p><strong>Add command output into an array<\/strong> &#8211; same thing, you don&#8217;t need loop<\/p>\n\n\n\n<p>Use bash internal command readarray. -t option removes trailing newline. More options look in <code>man bash<\/code>. Following will read each line of ps -ef into indexed array named <em>indexed_array<\/em>. Note the double &lt; &lt; separated with space. This is called <a href=\"https:\/\/tldp.org\/LDP\/abs\/html\/process-sub.html\">process substitution<\/a>. This feeds command output into stdin of the readarray command. NB! dont leave space between last &lt; and the brackets. <\/p>\n\n\n\n<pre id=\"block-86a5d521-b9df-49ff-95a5-fed57f0ebe54\" class=\"wp-block-preformatted\">readarray -t indexed_array &lt; &lt;(ps -ef)<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>If you found this useful, say thanks, click on some banners or <a href=\"http:\/\/www.bashpi.org\/?page_id=105\">donate<\/a>, I can always use some beer money.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two types of arrays you can use &#8211; indexed and associative arrays. Start by declaring the arrays $ declare -a indexed_array $ declare -A associative_array Add values to arrays &#8211; note the possibility to add values to arrays with += operator. $ indexed_array[0]=&#8221;value1&#8243; $ indexed_array[1]=&#8221;value2&#8243; $ indexed_array[2]=&#8221;value3&#8243; $ indexed_array+=&#8221;value4&#8243; $ indexed_array+=&#8221;value5&#8243; $ associative_array[one]=&#8221;value1&#8243; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-866","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=866"}],"version-history":[{"count":7,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/866\/revisions"}],"predecessor-version":[{"id":1220,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/866\/revisions\/1220"}],"wp:attachment":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}