58
votes
10answers
48k views

How to define hash tables in bash?

Just what title says. I am surprised by insufficiency of results in Google search for this question! What I want to is the equivalent of Python dictionaries but in bash (and hence, should work across ...
47
votes
2answers
15k views

How to iterate over associative array in bash

Based on an assoziative array in a bash script I need to iterate over it to get key & value. #!/bin/bash declare -A array array[foo]=bar array[bar]=foo I actually don't understand how to get ...
33
votes
12answers
41k views

Associative arrays in Shell scripts

We required a script that simulates Associative arrays or Map like data structure for Shell Scripting, any body?
7
votes
5answers
3k views

How to pass an associative array as argument to a function in Bash?

How do you pass an associative array as an argument to a function? Is this possible in Bash? The code below is not working as expected: function iterateArray { local ADATA="${@}" # ...
6
votes
4answers
3k views

Bash: How to assign an associative array to another variable name (e.g. rename the variable)?

I need to loop over an associative array and drain the contents of it to a temp array (and perform some update to the value). The leftover contents of the first array should then be discarded and i ...
5
votes
3answers
179 views

Bash scripting - Iterating through “variable” variable names for a list of associative arrays

I've got a variable list of associative arrays that I want to iterate through and retrieve their key/value pairs. I iterate through a single associative array by listing all its keys and getting the ...
3
votes
2answers
1k views

Bash 4 associative arrays: error “declare: -A: invalid option”

I've written a script that uses associative arrays in bash (v 4). It works fine on my local machine which is using 4.1.5(1)-release On the production machine, using 4.1.0(1)-release the following ...
3
votes
2answers
6k views

BASH: need some help with multidimensional associative arrays

I'm trying to create a multidimensional associative array but need some help. I have reviewed the page suggested in this SO answer but it confused me even more. So far here is what I have: The ...
2
votes
2answers
60 views

easy way to cleanly dump contents of associative array in bash?

In zsh I can easily dump the contents of an associative array with a single command: zsh% typeset -A foo zsh% foo=(a 1 b 2) zsh% typeset foo foo=(a 1 b 2 ) However despite searching high and low, ...
1
vote
2answers
3k views

bash populate an array in loop

How can i populate an array in loop? I'd like to do something like that: declare -A results results["a"]=1 results["b"]=2 while read data; do results[$data]=1 done for i in "${!results[@]}" do ...
1
vote
2answers
2k views

Bash associative array sorting

I get the following output: Pushkin - 100500 Gogol - 23 Dostoyevsky - 9999 Which is the result of the following script: for k in "${!authors[@]}" do echo $k ' - ' ${authors["$k"]} done ...
1
vote
1answer
87 views

Storing bash associate-array's values in while loop

I'm trying to write a script that will scan for bluetooth devices and keep track of the name, mac and time the device was first and last seen. I'm running into an issue storing the data into the ...
0
votes
3answers
2k views

Using variable as a key in an bash associative array

I'm trying to read the English dictionary in Linux into an associative array, using the words as keys and and predefined string as a value. This way I can look up words by key to see if they exist. ...
0
votes
2answers
562 views

value to great for base error from bash associative array

I have a file called a.txt which looks as follow 7A1123123asd 14 8aasdasdasd 15 Now I wrote some bash code to read the file and build an associative array from the values in it #!/bin/bash ...