Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Bash 4</h1> <p>Bash 4 natively supports this feature. Make sure your script's hashbang is <code>#!/usr/bin/env bash</code> or <code>#!/bin/bash</code> or anything else that references <code>bash</code> and not <code>sh</code>. Make sure you're executing your script, and not doing something silly like <code>sh script</code> which would cause your <code>bash</code> hashbang to be ignored. This is basic stuff, but so many keep failing at it, hence the re-iteration.</p> <p>You declare an associative array by doing:</p> <pre><code>declare -A animals </code></pre> <p>You can fill it up with elements using the normal array assignment operator:</p> <pre><code>animals=( ["moo"]="cow" ["woof"]="dog") </code></pre> <p>Or merge them:</p> <pre><code>declare -A animals=( ["moo"]="cow" ["woof"]="dog") </code></pre> <p>Then use them just like normal arrays. <code>"${animals[@]}"</code> expands the values, <code>"${!animals[@]}"</code> (notice the <code>!</code>) expands the keys. Don't forget to quote them:</p> <pre><code>echo "${animals[moo]}" for sound in "${!animals[@]}"; do echo "$sound - ${animals[$sound]}"; done </code></pre> <h1>Bash 3</h1> <p>Before bash 4, you don't have associative arrays. <strong>Do not use <code>eval</code> to emulate them</strong>. You must avoid eval like the plague, because it <em>is</em> the plague of shell scripting. The most important reason is that you don't want to treat your data as executable code (there are many other reasons too).</p> <p><em>First and foremost</em>: Just consider upgrading to bash 4. Seriously. <em>The future is now</em>, stop living in the past and <strong>suffering from it</strong> by forcing stupid broken and ugly hacks on your code and every poor soul stuck maintaining it.</p> <p>If you have some silly excuse why you "<em>can't upgrade</em>", <code>declare</code> is a far safer option. It does not evaluate data as bash code like <code>eval</code> does, and as such it does not allow arbitrary code injection quite so easily.</p> <p>Let's prepare the answer by introducing the concepts:</p> <p>First, indirection (seriously; never use this unless you're mentally ill or have some other bad excuse for writing hacks).</p> <pre><code>$ animals_moo=cow; sound=moo; i="animals_$sound"; echo "${!i}" cow </code></pre> <p>Secondly, <code>declare</code>:</p> <pre><code>$ sound=moo; animal=cow; declare "animals_$sound=$animal"; echo "$animals_moo" cow </code></pre> <p>Bring them together:</p> <pre><code># Set a value: declare "array_$index=$value" # Get a value: arrayGet() { local array=$1 index=$2 local i="${array}_$index" printf '%s' "${!i}" } </code></pre> <p>Let's use it:</p> <pre><code>$ sound=moo $ animal=cow $ declare "animals_$sound=$animal" $ arrayGet animals "$sound" cow </code></pre> <p>Note: <code>declare</code> cannot be put in a function. Any use of <code>declare</code> inside a bash function turns the variable it creates <em>local</em> to the scope of that function, meaning we can't access or modify global arrays with it. (In bash 4 you can use declare -g to declare global variables - but in bash 4, you should be using associative arrays in the first place, not this hack.)</p> <h1>Summary</h1> <p>Upgrade to bash 4 and use <code>declare -A</code>. If you can't, consider switching entirely to <code>awk</code> before doing ugly hacks as described above. And definitely stay the heck away from <code>eval</code> hackery.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload