1

How can I declare an associative array with something like

declare -A ENV_JOBS=( [""]="" ["staging"]="staging-job-name ["dev"]="dev-job-name" )

So whenever it gets an empty string it'll return empty value?

Currently the error message bad array subscript shows up when I declare and access with ${ENV_JOBS[""]}

My usage is to do something like "job-name${ENV_JOBS[$ENV]}" to be able to compute all env job names.

1

1 Answer 1

1

This is a limitation of bash. To work around it, add an extra character so it will never be empty.

declare -A ENV_JOBS=( ["@"]="" ["@staging"]="staging-job-name" ["@dev"]="dev-job-name" )
jobname=job-name${ENV_JOBS["@$ENV"]}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, yes the background context is bash. Just found that zsh (my cli but not scripting) is able to do something like declare -A ENV_JOBS=( [""]="" ). If people scripting in zsh might find it helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.