Creating and using a user-defined hash table in a Bash script

Posted on 2008-11-14
Last Modified: 2013-12-26

Can anyone explain to me how to create (or simulate) a hash table in a Bash script in Linux?  I know that Bash uses some internal hash tables to store commands, but I need to create my own.


Question by:doug5516
    LVL 48

    Expert Comment

    Why do you "need" to create your own?

    Do you have a specific problem you are trying to solve?

    Author Comment

    Hi, I need to create my own because as far as I know Bash does not have a hash data structure type (nor many other types that would be useful).  I need to be able to store and recall data using nonnumerical keys, therefore the need of a hash table.  I ended up using associative arrays, which while not the besh solution, worked for what I was trying to accomplish.
    LVL 48

    Expert Comment

    Associate arrays are just another name for a hash.

    Again, without knowing what you really are trying to achieve, it is difficult to give you guidance.

    If you really do need a hash, you are much better off using awk or Perl.

    Author Comment

    I do not think that I would agree with you that associate arrays are just another name for a hash.  I would agree that associate arrays are one particular implementation of a hash (and not the best one) just as using a hash function, an array, and some collision policy would be another.  If you are trying to tell me that there is no good way to implement a hash table in bash and that I should consider using another language then your point is well taken.
    LVL 1

    Accepted Solution

    This isn't at all a hash but it almost acts like one... does this help:
    typeset hash_${key}=$val
    typeset hash_${key}=$val
    ### Tests ###
    echo $hash_first
    # produces => 3
    echo "$(eval echo \$hash_${key})"
    # produces => 3
    echo $hash_second
    # produces => sam

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now