简体   繁体   中英

Substituting variable values in a string

I have a file (test.txt) with a line like this (verbatim):

MyCode $Cores

In a bash script, I have set the value of $Cores, so I want to execute the line from test.txt using this value. I tried this:

Cores=2
ProgramString=... read line from test.txt...
ProgramCommand=$(eval echo \$$ProgramString)
echo ${ProgramCommand}
${ProgramCommand}

The output is this:

$MyCode 2

It has successfully replaced $Cores with 2, but it has added a dollar sign at the beginning, so of course I get

$MyCode No such file or directory

Can anyone explain where this extra $ is coming from (and how to get rid of it)?

You included the $ with \\$ . Use ProgramCommand=$(eval echo $ProgramString) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM