0

I'm trying to create an array of arrays in bash. In ruby it would something like:

projects = [
  ["PROJECT_ID_1", "PROJECT_TOKEN_1"],
  ["PROJECT_ID_2", "PROJECT_TOKEN_2"],
  ["PROJECT_ID_3", "PROJECT_TOKEN_3"],
  ["PROJECT_ID_4", "PROJECT_TOKEN_4"]
]

This obviously doesn't work in bash but I can't quite get the syntax right. I know that I'll have to do something like:

declare -a PROJECTS=()
# What goes here?

What is the correct way of doing this in bash?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • 1
    You can't. There's no such native object. – Charles Duffy Jan 13 '17 at 18:40
  • btw -- all-caps names are used for variables with meaning to the system or shell, whereas lower-case names are [reserved for application use](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) -- that's explicitly specified for environment variables, but assigning to a shell variable will overwrite any like-named environment variable, so it applies in both places. – Charles Duffy Jan 13 '17 at 18:43
  • For your specific use case, use two separate native arrays: `project_ids=( A B C D ); project_tokens=( W X Y Z )`; then: `for idx in "${!project_ids[@]}"; do project_id=${project_ids[$idx}; project_token=${project_tokens[$idx]}; echo "Project with id $project_id has token $project_token"; done` – Charles Duffy Jan 13 '17 at 18:46
  • Mind you, in bash 4.x you could also just use a single associative array to map straight from IDs to tokens: `declare -A project_tokens=( [A]=W [B]=X [C]=Y [D]=Z )`; then `"${!project_tokens[@]}"` will expand to the list of IDs, and `"${project_tokens[$id]}` will map an ID to a token. – Charles Duffy Jan 13 '17 at 18:47

0 Answers0