Kayan Kayan - 5 months ago 6
Bash Question

print a defined variable with preserving $

I would like to print 120 model list along with preserving the "$".

My script is:

#!/bin/sh
for i in {1..10};do
declare model="$"model$i
model_list=$(paste $model)
done
echo $model_list


After running it,

paste: $model1: No such file or directory
paste: $model2: No such file or directory
paste: $model3: No such file or directory
paste: $model4: No such file or directory
paste: $model5: No such file or directory
paste: $model6: No such file or directory
paste: $model7: No such file or directory
paste: $model8: No such file or directory
paste: $model9: No such file or directory
paste: $model10: No such file or directory


Desired output:

$model1 $model2 $model3 $model4 $model5 $model6 $model7 $model8 $model9 $model10

Answer

if you are interested in output instead of creating array the you can use below code

for i in {1..10};do
echo -n "\$model$i "
done
echo

or

for i in {1..10};do
echo -n '$model'$i' '
done
echo

or

for i in {1..10};do
model_list=$model_list" \$model$i"
done
echo $model_list