0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【bash】変数に代入した値を使って、変数名を作り、その値を参照する

0
Last updated at Posted at 2022-10-11

もっと良い書き方はないのでだろうか?
コメントいただいた内容で更新しました。すっきり!

bash-3.2$ ENV=QA
bash-3.2$ echo $ENV
QA

bash-3.2$ SECRET_QA='this is a secret for qa'
bash-3.2$ echo $SECRET_QA
this is a secret for qa

bash-3.2$ echo SECRET_${ENV}
SECRET_QA

bash-3.2$ echo \$SECRET_${ENV}
$SECRET_QA

bash-3.2$ eval echo \$SECRET_${ENV}
this is a secret for qa

もしくは

bash-3.2$ eval echo \"\$SECRET_${ENV}\"
bash-3.2$ eval "echo \"\$SECRET_${ENV}\""
bash-3.2$ eval 'echo "$SECRET_'"${ENV}"'"'
bash-3.2$ eval echo $"$"SECRET_${ENV}""
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?