How to expand shell variables in a text file?

前端 未结 10 1578
夕颜
夕颜 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:42

    If a Perl solution is ok for you:

    Sample file:

    $ cat file.sh
    spool on to '$HOME/logfile.log';
    login 'username' 'password';
    

    Solution:

    $ perl -pe 's/\$(\w+)/$ENV{$1}/g' file.sh
    spool on to '/home/user/logfile.log';
    login 'username' 'password';
    

提交回复
热议问题