Search code examples
shellechocsh

How to echo a variable (name of which contains another varibale)


I was writing a small shell script to read and print lines of a file.

#!/bin/csh

set IN_FILE=$1
set i=1
foreach line ( "`cat $IN_FILE`" )
  set F$i="`echo $line`"
  echo ${F${i}}
  @ i = $i + 1
end

Upon executing, I am getting the following error

Missing '}'.

I was wondering how to echo a variable, which contains another variable in its name.


Solution

  • I don't have csh, so tried this with bash. The content of test.sh is:

    i=1
    eval "F$i=\"this is a line\""
    eval echo "test: " \${F$i}
    echo "F1 is" $F1
    

    and it seems to work:

     /tmp$ ./test.sh
     test: this is a line
     F1 is this is a line