Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-07-2007, 01:47 PM   #1
LQ Newbie
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
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?
Old 02-07-2007, 03:37 PM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
Old 02-07-2007, 03:39 PM   #3
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,782

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell scripting question. $Linuxnoob Fedora 13 04-01-2006 02:12 PM
shell (bash) scripting question(s) boxerboy Programming 5 11-08-2005 04:51 PM
Bash shell scripting question. pete1234 Programming 11 09-25-2005 03:59 AM
Bash shell scripting question. Itsu Linux - General 3 04-30-2005 04:52 AM
Bash shell scripting question mehesque Programming 2 07-15-2004 11:54 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration