assign multiple rows value to a variable using eval

background : Solaris, ksh

metresult="ooo
> pp"
ts=89
eval append_${ts}="$metresult"
bash: pp: command not found

I want to create a variable which has in a part of its name a dynamically-established number (stored in another variable) usually I do this with eval command. The problem I have now is that I want to assign to this variable a multiple row value and I get the error you see above ("pp" is the word from the second row from $metresult value.

If I want to assign a single-row-value to such a variable using eval everything works fine :

 metresult="ooo"
ts=89
eval append_${ts}="$metresult"
echo $append_89
ooo

I do something wrong syntactically speaking and I wonder if you have another approach. I do not want workarounds with arrays a.s.o. I only want to know this : how to assign multiple rows value to a variable using eval command

THX

This is the culprit. The "eval" sends the line through the evaluation process a second time, but the first time it has already eaten away your quotation and this second time the newline terminates the line. You have to escape the quotation to protect it for the correct evaluation pass to interpret it.

Notice the difference between:

echo "abc"                # evaluated once, but the "" are missing in the output
echo "\"abc\""            # evaluated once, the inner "" are there, without escaping
eval echo "\"abc\""       # second evaluation ate the second pair of ""
eval echo "\\"abc\\""     # two evaluations need two-time escaped quoations

I hope this helps.

bakunin

This was tested in the 'ksh' shell in Solaris since thats what you posted as your environment, it concatenates the variable with two lines(metresult) with the variable(ts):

$ metresult="ooo\n> pp"
$ ts=89
$ ts=$ts$metresult
$ print $ts
89ooo
> pp

I'd use real array's if possible. Otherwise eval append_${ts}='"$metresult"' so that "$metresult" is intact during 2nd evaluation without any backslashes to worry about. when I have to use eval I usually single quote everything except what I specifically want evaluated prior. eval 'append_'$ts'="$metresult"'

This is correct, but in this case the single quotes are doing the same as the escaping backslashes: they prevent everything inside from being evaluated at the first pass - including the double quotes, which lose their special meaning to the shell when inside the single quotes.

You can watch this effect when using several numbers of eval's on the same line (which is - save for demonstration - of course of no practical value although syntactically correct). Correct escaping with several backslashes will manage even this tricky predicament, while single quotes will get interpreted away like double quotes do.

I hope this helps.

bakunin

Yes, Bakunin. This approach got my problem solved. I had to "save" a second pair of "" for the 2nd evaluation by using \ .

Many thanks!