Simple Variable substitution in ksh not working

Hi Gurus,
Not able to catch what's going wrong here. I just want to get output as "tree".

server:/mk/app/nexapp $ echo $SHELL
/usr/bin/ksh
server:/mk/app/nexapp $ export db_name1="tree"
server:/mk/app/nexapp $ export i=1

1st try:

server:/mk/app/nexapp $ echo $(db_name$i)
ksh: db_name1:  not found.

2nd try:

server:/mk/app/nexapp $ echo $((db_name$i))
ksh: tree: 0403-009 The specified number is not valid for this command.

3rd try:

server:/mk/app/nexapp $ echo $(`(db_name$i)`)
ksh: db_name1:  not found.

Please help me understand why substitution is not working here. and what could be done to get the output as tree

---------- Post updated at 01:55 AM ---------- Previous update was at 01:17 AM ----------

Hi,

Just do

echo $db_name$i

It would print fine.

mukesh.lalwani,
With any Korn shell, you can use arrays with a numeric subscript for subscripts from 0 to about 2047. With ksh93 and with bash you can use numeric subscripts with larger values or with string valued subscripts. The syntax is:

$ i=1
$ dbname[1]="tree"
$ echo "${dbname[$i]}"
tree
$ 

or for multiple array elements:

$ dbname=("forest" "tree")
$ for ((i = 0; i < ${#dbname[@]}; i++))
> do	printf 'dbname[%d]="%s"\n' "$i" "${dbname[$i]}"
> done
dbname[0]="forest"
dbname[1]="tree"
$ 

Or, with any shell based on Bourne shell syntax, you can use eval as shown in the link you referenced. Note that eval can be a security risk if any of the code that you are feeding to eval is supplied by a user. But, eval is generally safe if all of the strings being evaluated by eval are hardcoded into your script.

RTY,

$ echo $dbname$i

prints whatever $dbname expands to followed by whatever $i expands to not to the contents of the variable named by dbname followed by the expansion of $i .

And:

$ echo ${dbname$i}
ksh: syntax error: `$' unexpected
$ 

doesn't work either.

Well, substitution IS working here, but it may not yield what you expected.
$(db_name$i) is an instance of

$((db_name$i)) performs

$(`(db_name$i)`) is, mmmm, say, a duplicate command substitution. Results, if any, may be dubious.

---------- Post updated at 10:38 ---------- Previous update was at 10:34 ----------

And, the db_name is not evaluated as the $ sign is missing.

And, what do you expect "the output as tree" to look like?