Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1007211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:35:00+00:00 2026-05-16T08:35:00+00:00

What is the equivalent of Python dictionaries but in Bash (should work across OS

  • 0

What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:35:01+00:00Added an answer on May 16, 2026 at 8:35 am

    Bash 4

    Bash 4 natively supports this feature. Make sure your script’s hashbang is #!/usr/bin/env bash or #!/bin/bash so you don’t end up using sh. Make sure you’re either executing your script directly, or execute script with bash script. (Not actually executing a Bash script with Bash does happen, and will be really confusing!)

    You declare an associative array by doing:

    declare -A animals
    

    You can fill it up with elements using the normal array assignment operator. For example, if you want to have a map of animal[sound(key)] = animal(value):

    animals=( ["moo"]="cow" ["woof"]="dog")
    

    Or declare and instantiate in one line:

    declare -A animals=( ["moo"]="cow" ["woof"]="dog")
    

    Then use them just like normal arrays. Use

    • animals['key']='value' to set value

    • "${animals[@]}" to expand the values

    • "${!animals[@]}" (notice the !) to expand the keys

    Don’t forget to quote them:

    echo "${animals[moo]}"
    for sound in "${!animals[@]}"; do echo "$sound - ${animals[$sound]}"; done
    

    Bash 3

    Before bash 4, you don’t have associative arrays. Do not use eval to emulate them. Avoid eval like the plague, because it is the plague of shell scripting. The most important reason is that eval treats your data as executable code (there are many other reasons too).

    First and foremost: Consider upgrading to bash 4. This will make the whole process much easier for you.

    If there’s a reason you can’t upgrade, declare is a far safer option. It does not evaluate data as bash code like eval does, and as such does not allow arbitrary code injection quite so easily.

    Let’s prepare the answer by introducing the concepts:

    First, indirection.

    $ animals_moo=cow; sound=moo; i="animals_$sound"; echo "${!i}"
    cow
    

    Secondly, declare:

    $ sound=moo; animal=cow; declare "animals_$sound=$animal"; echo "$animals_moo"
    cow
    

    Bring them together:

    # Set a value:
    declare "array_$index=$value"
    
    # Get a value:
    arrayGet() { 
        local array=$1 index=$2
        local i="${array}_$index"
        printf '%s' "${!i}"
    }
    

    Let’s use it:

    $ sound=moo
    $ animal=cow
    $ declare "animals_$sound=$animal"
    $ arrayGet animals "$sound"
    cow
    

    Note: declare cannot be put in a function. Any use of declare inside a bash function turns the variable it creates local 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 can use associative arrays in the first place, avoiding this workaround.)

    Summary:

    • Upgrade to bash 4 and use declare -A for associative arrays.
    • Use the declare option if you can’t upgrade.
    • Consider using awk instead and avoid the issue altogether.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the Python equivalent of the following code in Ruby? def loop cont=nil
To do the equivalent of Python list comprehensions, I'm doing the following: some_array.select{|x| x
Is there a Ruby equivalent for Python's is? It tests whether two objects are
Is there any Ruby equivalent for Python's builtin zip function? If not, what is
What is the idiomatic Python equivalent of this C/C++ code? void foo() { static
Does anyone know of a Python equivalent for FMPP the text file preprocessor? Follow
I would like to do the equivalent off this (ruby code) in python for
I'm looking for a Ruby's equivalent of Code Like a Pythonista: Idiomatic Python Desirable
Is there an English equivalent to the French expression: “il faut d’abord apprendre à marcher avant de courir”?
Is there an equivalent for Interlocked.Exchange for boolean? Such as an atomic exchange of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.