0

I have an environment variable as

slack_auth_token_test="x0xb-xxxxx-xxxx"

my script

./script.sh test

slack_channel=${1}
echo "token for slack channel is"
echo "${slack_auth_token_[$slack_channel]}"

and I am passing the channel test as a parameter. So I am looking for a way to append this channel name test to the environment variable and get the token

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ajay
  • 55
  • 7
  • 1
    Does this answer your question? [Dynamic variable names in Bash](https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash) – tjm3772 Jun 22 '23 at 17:53
  • 1
    Make the name in a variable like: `varname=slack_auth_token_"$slack_channel"` and then you can reference the environment variable with `"${!varname}"` – tjm3772 Jun 22 '23 at 17:54
  • thank you @tjm3772 for the quick response. I forgot to put the `!` while calling the referenced variable – Ajay Jun 22 '23 at 17:56

1 Answers1

3

With bash, you can use a nameref:

$ slack_auth_token_test="x0xb-xxxxx-xxxx"
$ set -- test
$ declare -n token="slack_auth_token_$1"
$ echo "$token"
x0xb-xxxxx-xxxx
glenn jackman
  • 238,783
  • 38
  • 220
  • 352