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?

4
  • 1
    You can't. There's no such native object. Commented Jan 13, 2017 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 -- 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. Commented Jan 13, 2017 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 Commented Jan 13, 2017 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. Commented Jan 13, 2017 at 18:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.