2

This KornShell Script (ksh):

envir=Dev
eval "${envir}foo=bar"
echo "$Devfoo"

Output:

bar

But I will not know what value is assigned to envir variable. So I want to do something like this where the output is the same as above:

envir=Dev
eval "${envir}foo=bar"
echo "${${envir}foo}"

Output:

${${envir}foo}: bad substitution
5
  • is this ksh88 or ksh93? Commented Jul 28, 2015 at 16:55
  • Not sure, but I assume ksh88. Commented Jul 28, 2015 at 17:45
  • Try ksh --version or what $(which ksh) Commented Jul 28, 2015 at 17:56
  • --version: 0403-010 A specified flag is not valid for this command. The what $(which ksh) command has a lot of output. Not sure what to look for. Commented Jul 28, 2015 at 17:58
  • What would be a better title for this question? Commented Jul 28, 2015 at 18:31

1 Answer 1

2

For ksh93, you have (at least) a couple of choices

  1. associative arrays

    envir=Dev
    foo["$envir"]=bar
    echo "${foo["$envir"]}"
    
  2. namerefs

    nameref var=${envir}foo
    var=bar
    echo "$var"
    

For ksh88, you may be stuck with eval:

envir=Dev
name="${envir}foo"
eval "$name=bar"
eval "echo \$$name"
1
  • I must have ksh88 because only the eval solution worked.Thank You! ksh: Dev: 0403-009 The specified number is not valid for this command. ksh: nameref: not found. Commented Jul 28, 2015 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .