{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rednafi.com/misc/associative-arrays-in-bash/",
  "description": "Learn how to use associative arrays in Bash to create key-value pairs, mimic dictionaries, and manage complex data structures in shell scripts.",
  "path": "/misc/associative-arrays-in-bash/",
  "publishedAt": "2023-05-03T00:00:00.000Z",
  "site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
  "tags": [
    "Shell",
    "Unix",
    "TIL",
    "Data Structures"
  ],
  "textContent": "One of my favorite pastimes these days is to set BingChat to creative mode, ask it to teach\nme a trick about topic X, and then write a short blog post about it to reinforce my\nunderstanding. Some of the things it comes up with are absolutely delightful. In the spirit\nof that, I asked it _to teach me a Shell trick that I can use to mimic maps or dictionaries\nin a shell environment_. I didn't even know what I was expecting.\n\nIt didn't disappoint and introduced me to the idea of associative arrays in Bash. This data\nstructure is basically the Bash equivalent of a map.\n\nFirst, we have our usual arrays which are containers that can store multiple values, indexed\nby numbers. Associative arrays are similar, but they use strings as keys instead of numbers.\nFor example, if you want to store the names of some fruits in a regular array, you can use:\n\nThis will create an array called fruits with three elements. You can access the elements by\nusing the index number inside brackets, such as:\n\nThis prints:\n\nYou can also use a range of indices to get a slice of the array, such as:\n\nThis will print:\n\nMoreover, you can use ` or @ to get all the elements of the array, such as:\n\nThis returns:\n\nAssociative arrays are declared with the declare -A command, and then assigned values\nusing the = operator and brackets. For example, if you want to store the prices of some\nfruits in an associative array, you can use:\n\nOr you can create the key-value pairs in place like this:\n\nThis will create an associative array called prices with three key-value pairs. You can\naccess the values by using the keys inside brackets, such as:\n\nThis will print:\n\nSimilar to regular arrays, you can use  or @ to get all the keys or values of the\nassociative array. Run the following command to get all the keys of the prices associative\narray:\n\nThis will print:\n\nTo get the values, run:\n\nThis returns:\n\nArrays and associative arrays can be useful when you want to store and manipulate complex\ndata structures in bash. You can use them to perform arithmetic operations, string\noperations, or loop over them with for or while commands. For example, you can use:\n\nIn the above snippet, we iterate through the keys of prices in a for loop. The\n${!prices[]} notation expands to a list of all the keys in the prices array. Inside the\nloop, we print the key-value pairs, where $fruit represents the current key and\n${prices[$fruit]} represents the corresponding value. So in each iteration, the snippet\nwill output the name of each fruit along with its corresponding price.\n\nRunning the snippet will print:\n\nA more practical example\n\nHere's a script that downloads three famous RFCs using cURL. We're using an associative\narray for bookkeeping purposes.\n\nRunning this will download the RFCs in the current directory:\n\nThe script begins by declaring an associative array called rfc_urls. This array serves as\na convenient way to keep track of the RFCs we want to download. Each key in the array\nrepresents a unique identifier for an RFC, while the corresponding value holds the complete\nURL to download that specific RFC.\n\nInside a loop that iterates over the keys of the rfc_urls array, we retrieve the URL value\nassociated with each key. To provide a progress update, we echo a message indicating the RFC\nbeing downloaded.\n\nUsing the curl command with the options -OJLs, we initiate the download process. The\n-O flag ensures that the remote file is saved with its original filename, while the -J\nflag takes advantage of the Content-Disposition header in the HTTP response to determine\nthe filename. We include the -L flag to follow redirects, and the -s` flag to silence\ncurl's progress output.\n\nFurther reading\n\n- [Advanced Bash scripting guide - devconnected]\n\n- [Advanced Bash scripting techniques for Linux administrators]\n\n- [Useful Bash command line tips and tricks]\n\n- [Learning Bash with command line games]\n\n\n\n\n[advanced bash scripting guide - devconnected]:\n    https://devconnected.com/advanced-bash-scripting-guide/\n\n[advanced bash scripting techniques for linux administrators]:\n    https://tecadmin.net/advanced-bash-scripting-techniques/\n\n[useful bash command line tips and tricks]:\n    https://linuxconfig.org/useful-bash-command-line-tips-and-tricks-examples-part-1\n\n[learning bash with command line games]:\n    https://opensource.com/article/19/10/learn-bash-command-line-games",
  "title": "Associative arrays in Bash"
}