Skip to content

Instantly share code, notes, and snippets.

@dlangille
Created September 11, 2021 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlangille/ae407b1e7c3f52897114a580741ab3a9 to your computer and use it in GitHub Desktop.
Save dlangille/ae407b1e7c3f52897114a580741ab3a9 to your computer and use it in GitHub Desktop.
Using variables for variables in a bourne shell script
Based on this Twitter thread: https://twitter.com/DLangille/status/1436388549985771520
In a bourne shell, with VAR=ab and ab=123, can I get at the 123 value using ony VAR and not ab?
e.g. echo $VAR gives ab, but I want 123
It's not ${!VAR} because I think that's bash.
$ echo ${!VAR}
/bin/sh: ${!V...}: Bad substitution
#!/bin/sh
INGRESS_JAIL="ingress01"
WEB_JAIL="nginx01"
PG_JAIL="pg01"
DEFAULT_REPO_TREE="130amd64-default-primary"
eval ${INGRESS_JAIL}_REPO_TREE="${DEFAULT_REPO_TREE}"
eval ${WEB_JAIL}_REPO_TREE="${DEFAULT_REPO_TREE}"
eval ${PG_JAIL}_REPO_TREE="130amd64-default-pg13"
JAILS="$INGRESS_JAIL $WEB_JAIL $PG_JAIL"
for jail in ${JAILS}
do
eval repo_tree=\$${jail}_REPO_TREE
echo $jail will use $repo_tree
done
$ ~/tmp/vars.sh
ingress01 will use 130amd64-default-primary
nginx01 will use 130amd64-default-primary
pg01 will use 130amd64-default-pg13
Success. Thank you for coming to my TED talk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment