How to expand shell variables in a text file?

前端 未结 10 1475
夕颜
夕颜 2021-02-04 02:05

Consider a ASCII text file (lets say it contains code of a non-shell scripting language):

Text_File.msh:

spool on to \'$LOG_FILE_PATH/lo         


        
10条回答
  •  无人共我
    2021-02-04 02:43

    If you want it in one line (I'm not a bash expert so there may be caveats to this but it works everywhere I've tried it):

    when test.txt contains

    ${line1}
    ${line2}
    

    then:

    >line1=fark
    >line2=fork
    >value=$(eval "echo \"$(cat test.txt)\"")
    >echo "$value"
    line1 says fark
    line2 says fork
    

    Obviously if you just want to print it you can take out the extra value=$() and echo "$value".

提交回复
热议问题