0

I have a bash script which basically contains all export variables and I am trying to add associative array into that script. This is my export's script:

#!/bin/bash
export declare -A oldLinks
oldLinks["A"]="linkA"
oldLinks["B"]="linkB"
oldLinks["C"]="linkC"
oldLinks["D"]="linkD"

export declare -A newLinks
newLinks["E"]="linkE"
newLinks["F"]="linkF"
newLinks["G"]="linkG"
newLinks["H"]="linkH"

This is the main script:

#!/bin/bash
source ArraysFile
for i in "${!oldLinks[@]}"
do
  echo "${i} -> ${oldLinks[$i]}"
done

for i in "${!newLinks[@]}"
do
  echo "${i} -> ${newLinks[$i]}"
done

This is the error which I'm getting:

export: `-A': not a valid identifier
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Vin
  • 177
  • 3
  • 11
  • 4
    You can't say `export declare ...`, but `declare` had the `-x` option for exporting. `declare -xA oldLinks`. However, you don't need to export the arrays to use them in the main script. – ooga Jun 20 '19 at 01:38
  • 1
    The reason you don't need to `export` anything is that you `source` ArraysFile, so everything is running in the same shell process. – Gordon Davisson Jun 20 '19 at 05:56
  • Does this answer your question? [How to export an associative array (hash) in bash?](https://stackoverflow.com/questions/12944674/how-to-export-an-associative-array-hash-in-bash) – Ingo Karkat Aug 09 '21 at 19:30

0 Answers0