-   Programming (
-   -   Bash Shell Scripting Dynamic Variable naming question (

ZuG 02-07-2007 01:47 PM

Bash Shell Scripting Dynamic Variable naming question

I'm trying to name a variable dynamically such that the name is the contents of another variable plus some text.


variable $SITE=
I want to create variable called "" using the $SITE variable and then assign some value to it.

Something like:


but of course that doesn't work. I get
bash: command not found.

How do I get it right? 02-07-2007 03:37 PM

Know two things.

First, a symbol in bash may not contain colons, slashes, or periods. Period. I recommend you change them all to underscores, or get rid of them altogether.

Second, bash can get confused if you do something like


no matter what the current value of $SYMBOL is. You can get around that by using the "set" command (or the "export" command if you wish this symbol to be accessible by programs you run after that).

I recommend you play with something like this:


NEWSITE=$(echo $SITE | sed -e 's/[^A-Za-z0-9]/_/g' -e 's/$/DOWN/')
set ${NEWSITE}=no

Hope this helps.

jlliagre 02-07-2007 03:39 PM

This is generally doable, but you first need to use a valid variable name, and "" is not.

That one would work:

$ SITE=http_10_0_0_1
$ eval ${SITE}DOWN=no
$ eval echo ${SITE}DOWN
$ eval echo \$${SITE}DOWN
$ echo $http_10_0_0_1DOWN

All times are GMT -5. The time now is 12:06 PM.