Questions tagged [associative-array]
61 questions
33
votes
6 answers
BASH associative array printing
Is there a way to print an entire array ([key]=value) without looping over all elements?
Assume I have created an array with some elements:
declare -A array
array=([a1]=1 [a2]=2 ... [b1]=bbb ... [f500]=abcdef)
I can print back the entire array…
Dani_l
- 4,720
- 1
- 18
- 34
19
votes
1 answer
How to use associative arrays safely inside arithmetic expressions?
A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3.
A common use is for counting occurrences of some strings.
However, I find that…
Stéphane Chazelas
- 522,931
- 91
- 1,010
- 1,501
13
votes
4 answers
Associative Arrays in Shell Scripts
I saw a trick for implementing associative arrays in a shell script. For example print array["apples"] could be scripted as echo \$array$key where key=apples.
However, there was no mention of how to generate the keys to iterate over the array.
The…
EggHead
- 275
- 1
- 4
- 8
10
votes
4 answers
Inverting an associative array
Let's say I have an associative array in bash,
declare -A hash
hash=(
["foo"]=aa
["bar"]=bb
["baz"]=aa
["quux"]=bb
["wibble"]=cc
["wobble"]=aa
)
where both keys and values are unknown to me (the actual data is read from…
Kusalananda
- 320,670
- 36
- 633
- 936
10
votes
1 answer
Pass associative array as parameter list to script
In a script I have an associative array like:
declare -A VARS=( ["key1"]="value1" ["key2"]="value" )
Is there a single command to transform that into a parameter list in the form
--key1=value1 --key2=value2
without having to manually re-write
…
Matteo Tassinari
- 215
- 3
- 11
10
votes
4 answers
How to read a properties file into an associative array?
I'd like to read the properties in a properties file into an associative array. How can I do it?
Specifics on what to parse: hash and equal signs. Everything else is a bonus.
Properties file content example:
#…
AlikElzin-kilaka
- 561
- 1
- 8
- 20
8
votes
1 answer
In ZSH, how do I unset an arbitrary associative array element?
An associative array I have has arbitrary keys, including keys containing backquotes, brackets, etc:
$ typeset -A arr
$ key='`'
$ arr[$key]=backquote
$ echo $arr[$key]
backquote
I now need to unset arr[$key]. Looking through the web, I tried:
$…
Michaël
- 774
- 5
- 18
7
votes
3 answers
Getting the key corresponding to a particular value in an associative array
I declare an associative array:
declare -A array=([a]=blue [b]=red [c]=yellow)
now I can do:
echo ${array[@]} --> blue red yellow
or
echo ${array[b]} --> red
or
echo ${!array[@]} --> a b c
Now I want to know only the key associated to red…
pietro letti
- 419
- 1
- 5
- 11
7
votes
1 answer
Bash return an associative array from a function and then pass that associative array to other functions
I'm trying to build an associative array in a function from a list pass in via an arg, but It just isn't working:
#!/usr/bin/env bash
function cwd {
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
}
function list_commands {
local…
Thermatix
- 351
- 4
- 14
7
votes
4 answers
Return the output of a command into an associative array
I need to put the output of a command into an associative array.
For example:
dig mx +short google.com
Will return:
20 alt1.aspmx.l.google.com.
40 alt3.aspmx.l.google.com.
50 alt4.aspmx.l.google.com.
10 aspmx.l.google.com.
30…
fidelity
- 73
- 1
- 4
7
votes
3 answers
Set awk array on command line?
I know the -v switch can be used to awk on the command line to set the value for a variable.
Is there any way to set values for awk associative array elements on the command line?
Something like:
awk -v myarray[index]=value -v myarray["index…
Wildcard
- 35,316
- 26
- 130
- 258
7
votes
1 answer
Iterate bash associative array in Makefile
$ bash -version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Consider the following shell script:
#!/bin/bash
declare -A PROVS=( ["NL"]=10 ["PE"]=11 ["NS"]=12 ["NB"]=13 ["QC"]=24 ["ON"]=35 ["MB"]=46 ["SK"]=47 ["AB"]=48 ["BC"]=59…
brian
- 73
- 1
- 1
- 4
6
votes
2 answers
How to set environment variables dynamically for command?
I would like to use some kind of minimalistic template engine with pure bash and envsubst:
user@host:~$ env -i FOO=foo BAR="bar baz" envsubst '$FOO,$BAR' \
<<< 'Hello "$FOO" and "$BAR"!'
Hello "foo" and "bar baz"!
Above works, but only contains…
user00705358365913
- 63
- 5
6
votes
2 answers
Bash: value too great for base when using a date as array key
I have read about specifying "10#", but I don't think it's my case as I am not doing number comparison. I am trying to create an associative array in Bash, and the code worked fine until today (2021-02-08):
dailyData["$today"]="$todayData"
$today…
Polizi8
- 175
- 2
- 11
5
votes
2 answers
Set a key with spaces in an associative array in Zsh
In Bash (4 or above), if I have an associative array dict, I can set its value like dict[apple count]=1 and I would be able to access it with ${dict[apple count]}. Does Zsh allow space in key names? dict[apple count]=1 doesn’t work in Zsh, so I…
Franklin Yu
- 1,227
- 12
- 23