eval is one of the most abused, and least secure, features in scripting. And in modern shells it's nearly always completely unnecessary. Most people who do so are usually trying to set up some kind of indirect or dynamic variable referencing, which can almost always be done more cleanly with an array of some kind.
How can I use variable variables (indirect variables, pointers, references) or associative arrays?
http://mywiki.wooledge.org/BashFAQ/006
As you have discovered, one of the big problems with trying to create dynamic variable names is that variable names can only contain letters, numbers, and the underscore, and cannot begin with a number (P.S. only the
name has this restriction, not the contents). This can be a difficult restriction to overcome when trying to work with arbitrary text. That's what makes associative arrays so useful, as you can use any arbitrary text string as the index to an array element, even one stored in another variable or array.
And read here to see what the stink is with eval:
Eval command and security issues
http://mywiki.wooledge.org/BashFAQ/048
The short form:
never use
eval on any line that has variables with unknown content or commands that produce unknown output. If you don't know
exactly what the
eval'd command is doing at all times, you shouldn't be using it at all.
Edit: BTW, you should really take another look at millgate's first suggestion and just
source the file (after fixing the contents to contain properly-defined variable names). Sourcing means incorporating an external file into your script, as if it were written there directly, and so all commands and variable settings in it will be executed at the position of the sourcing.
The source command is written simply as ".":
Code:
if [[ -r testfile ]]; then
. testfile
else
echo "testfile not found or readable."
exit 1
fi
echo "${apache[@]}"
echo "${apache_ant[@]}"