[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3823
  • Last Modified:

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

Hi,

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.

Thanks,

Doug
0
doug5516
Asked:
doug5516
  • 2
  • 2
1 Solution
 
TintinCommented:
Why do you "need" to create your own?

Do you have a specific problem you are trying to solve?
0
 
doug5516Author Commented:
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.
0
 
TintinCommented:
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.
0
 
doug5516Author Commented:
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.
0
 
JoshavenCommented:
This isn't at all a hash but it almost acts like one... does this help:
key="first"
val=3
typeset hash_${key}=$val
key="second"
val="sam"
typeset hash_${key}=$val
 
### Tests ###
echo $hash_first
# produces => 3
 
key='first'
echo "$(eval echo \$hash_${key})"
# produces => 3
 
echo $hash_second
# produces => sam

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now