{"id":30221,"date":"2026-04-29T14:31:36","date_gmt":"2026-04-29T12:31:36","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=30221"},"modified":"2026-04-24T14:37:08","modified_gmt":"2026-04-24T12:37:08","slug":"bash-array-guide","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/bash-array-guide\/","title":{"rendered":"Bash Array Guide: Indexed, Associative, Loops"},"content":{"rendered":"\n<p>Bash arrays are one of those features that look intimidating until you use them once. After that, you&#8217;ll find yourself reaching for them constantly when writing scripts that handle lists of values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-a-bash-array\">What Is a Bash Array?<\/h2>\n\n\n\n<p>A bash array is a variable that holds multiple values. Instead of declaring separate variables for each item in a list, you put them in an array and reference them by index or key. Bash doesn&#8217;t care whether elements are strings or numbers, and it doesn&#8217;t enforce consistent types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-indexed-vs-associative-arrays\">Indexed vs Associative Arrays<\/h3>\n\n\n\n<p>Indexed arrays use integers as keys, starting from 0. They&#8217;re the default and behave like arrays in most other languages. Associative arrays use strings as keys, giving you key-value pair functionality similar to hash maps or dictionaries. Associative arrays require explicit declaration with declare -A.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-when-to-use-arrays-in-bash-scripts\">When to Use Arrays in Bash Scripts<\/h3>\n\n\n\n<p>Use arrays when you&#8217;re iterating over a list of servers, files, usernames, or any collection of items. Without arrays, you end up with multiple variables (server1, server2, server3) and conditional logic that duplicates code. With an array, one loop handles the entire list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-declare-a-bash-array\">How to Declare a Bash Array<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-declaring-an-indexed-array\">Declaring an Indexed Array<\/h3>\n\n\n\n<p>Three ways to declare an indexed array:<\/p>\n\n\n\n<p>All at once:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits=(\"apple\" \"banana\" \"cherry\")<\/code><\/pre>\n\n\n\n<p>One by one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits&#91;0]=\"apple\" fruits&#91;1]=\"banana\" fruits&#91;2]=\"cherry\"<\/code><\/pre>\n\n\n\n<p>Using declare:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>declare -a fruits=(\"apple\" \"banana\" \"cherry\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-declaring-an-associative-array\">Declaring an Associative Array<\/h3>\n\n\n\n<p>Associative arrays require the declare -A directive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>declare -A servers servers&#91;web]=\"192.168.1.10\" servers&#91;db]=\"192.168.1.20\" servers&#91;cache]=\"192.168.1.30\"<\/code><\/pre>\n\n\n\n<p>Or inline:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>declare -A servers=(&#91;web]=\"192.168.1.10\" &#91;db]=\"192.168.1.20\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-add-variables-to-a-bash-array\">How to Add Variables to a Bash Array<\/h2>\n\n\n\n<p>For indexed arrays, append using the += operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits+=(\"date\" \"elderberry\")<\/code><\/pre>\n\n\n\n<p>Assign to a specific index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits&#91;5]=\"fig\"<\/code><\/pre>\n\n\n\n<p>Note: bash arrays are sparse. Assigning to index 5 when you only have 3 elements leaves indices 3 and 4 unset, not empty.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-referencing-and-printing-array-elements\">Referencing and Printing Array Elements<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-access-a-single-element\">Access a Single Element<\/h3>\n\n\n\n<p>Use the curly brace syntax with the index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${fruits&#91;0]}<\/code><\/pre>\n\n\n\n<p>For associative arrays:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${servers&#91;web]}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-print-the-entire-array\">Print the Entire Array<\/h3>\n\n\n\n<p>The @ symbol expands all elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${fruits&#91;@]}<\/code><\/pre>\n\n\n\n<p>To see all keys:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${!fruits&#91;@]}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-get-the-array-length\">Get the Array Length<\/h3>\n\n\n\n<p>Prefix with # to get the bash array length:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${#fruits&#91;@]}<\/code><\/pre>\n\n\n\n<p>For a specific element&#8217;s string length:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${#fruits&#91;0]}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-remove-bash-array-elements\">How to Remove Bash Array Elements<\/h2>\n\n\n\n<p>Use unset to remove a specific element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unset fruits&#91;1]<\/code><\/pre>\n\n\n\n<p>This removes the element but leaves the index gap. The array length decreases by one. To remove the entire array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unset fruits<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-loop-through-a-bash-array\">How to Loop Through a Bash Array<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-loop-with-for-in\">Loop with For-In<\/h3>\n\n\n\n<p>The standard way to iterate over array values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for fruit in \"${fruits&#91;@]}\"; do \u00a0 \u00a0 echo \"$fruit\" done<\/code><\/pre>\n\n\n\n<p>The quotes around ${fruits[@]} handle elements with spaces correctly. Without them, an element like &#8216;passion fruit&#8217; splits into two.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-loop-with-index\">Loop with Index<\/h3>\n\n\n\n<p>When you need the index alongside the value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in \"${!fruits&#91;@]}\"; do \u00a0 \u00a0 echo \"Index $i: ${fruits&#91;$i]}\" done<\/code><\/pre>\n\n\n\n<p>The ! prefix expands the array&#8217;s indices rather than its values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-passing-a-bash-array-to-a-function\">Passing a Bash Array to a Function<\/h2>\n\n\n\n<p>Bash doesn&#8217;t natively pass arrays to functions. The workaround: pass the array name and use indirect expansion inside the function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function process_array { \u00a0 \u00a0 local -n arr=$1 \u00a0 \u00a0 for item in \"${arr&#91;@]}\"; do \u00a0 \u00a0 \u00a0 \u00a0 echo \"Processing: $item\" \u00a0 \u00a0 done }\u00a0 my_list=(\"file1\" \"file2\" \"file3\") process_array my_list<\/code><\/pre>\n\n\n\n<p>The -n flag on local creates a name reference. The function operates on the original array without copying it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faq-bash-array\">FAQ: Bash Array<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777034061991\"><strong class=\"schema-faq-question\">How do I declare an array in bash?<\/strong> <p class=\"schema-faq-answer\">For an indexed array: fruits=(&#8220;apple&#8221; &#8220;banana&#8221; &#8220;cherry&#8221;) For an associative array: declare -A servers=([web]=&#8221;192.168.1.10&#8243; [db]=&#8221;192.168.1.20&#8243;)<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777034070802\"><strong class=\"schema-faq-question\">How do I loop through a bash array?<\/strong> <p class=\"schema-faq-answer\">Use for-in: for item in &#8220;${array[@]}&#8221;; do echo &#8220;$item&#8221;; done. Always quote &#8220;${array[@]}&#8221; to handle elements containing spaces.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777034079268\"><strong class=\"schema-faq-question\">How do I get the length of a bash array?<\/strong> <p class=\"schema-faq-answer\">Use ${#array[@]}. Example: count=${#fruits[@]}. This gives the number of elements in the array.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777034086608\"><strong class=\"schema-faq-question\">What is a bash associative array?<\/strong> <p class=\"schema-faq-answer\">An associative array uses string keys instead of integer indices. Declare it with declare -A: declare -A config=([host]=&#8221;localhost&#8221; [port]=&#8221;3306&#8243;). Access values with ${config[host]}.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash arrays are one of those features that look intimidating until you use them once. After that, you&#8217;ll find yourself reaching for them constantly when writing scripts that handle lists of values. What Is a Bash Array? A bash array is a variable that holds multiple values. Instead of declaring separate variables for each item [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[3402],"class_list":["post-30221","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Milan Ivanovic","author_link":"https:\/\/contabo.com\/blog\/author\/milan\/"},"uagb_comment_info":0,"uagb_excerpt":"Bash arrays are one of those features that look intimidating until you use them once. After that, you&#8217;ll find yourself reaching for them constantly when writing scripts that handle lists of values. What Is a Bash Array? A bash array is a variable that holds multiple values. Instead of declaring separate variables for each item&hellip;","authors":[{"term_id":3402,"user_id":0,"is_guest":1,"slug":"contabro","display_name":"ContaBro","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=30221"}],"version-history":[{"count":1,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30221\/revisions"}],"predecessor-version":[{"id":30222,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30221\/revisions\/30222"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=30221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=30221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=30221"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=30221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}