ksh escape a character

friends,

I have a situation where i am using a $RANDOM function along with the filename, I want this to be escaped by the OS in the first assignment (works as expected) and executed in the second assignment (does not execute $RANDOM)

filename1=filename1_\$RANDOM
echo $filename1
filename2=filename1
echo $filename2

The output of $filename1 is filename1_$RANDOM
The output of $filename2 is filename1_$RANDOM (i am expecting the $RANDOM to be substituted by a random number in the second assignment, since there is no escape character not sure why the shell is not substituting $RANDOM with a number)

Try

filename2=$(eval echo $filename1)
1 Like

Why do you need dynamic variable names? Time and time again we get asked for this, and there's extremely rarely any good reason to do this. It's insecure and error-prone.

Thanks, It works perfect !