I want to be able to do:
Script1.sh
declare -A map=(
['A']=1
['B']=2
['C']=3
['D']=4
)
sh script2.sh ???
Script2.sh
params = ...
echo ${params['A']}
ie, access parameters by keys. I have seen related questions for normal arrays and the answer to them has been to pass the array as:
sh script2.sh "${AR[@]}"
which I believe translates to:
sh script2.sh "${map[0]}" "${map[1]}" "${map[2]}"
But with that, I can only access the elements based on their order.
Is there a clever trick to achieve what I want? perhaps with something that passes on "A=1" "B=2" "C=3" "D=4" instead and have script2.sh parse them? or is there a neater solution?
shdoesn’t have arrays.params = ...is invalid syntax orshandbash. Did you check your code at shellcheck.net ? Is there a reason you're not using "shebang" lines at the top of each script to control what shell interprets your code (i.e.#!/bin/bash), rather than your current code,bash script2? If readers edit your Q, then there's a risk of making a change that doesn't help define your problem. Clearly defining your problem is your responsibility, right? Good luck.