sed中的環境變量替換 - Environment variable substitution in sed

問題:

If I run these commands from a script:如果我從腳本運行這些命令:

#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla

it is fine.沒事。

But, if I run:但是,如果我運行:

#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown option to `s' 

I read in tutorials that to substitute environment variables from shell you need to stop, and 'out quote' the $varname part so that it is not substituted directly, which is what I did, and which works only if the variable is defined immediately before.我在教程中讀到,要從 shell 替換環境變量,您需要停止,並“引用” $varname部分,以便它不會被直接替換,這就是我所做的,並且僅當變量在之前定義時纔有效.

How can I get sed to recognize a $var as an environment variable as it is defined in the shell?我怎樣才能讓 sed 將$var識別爲 shell 中定義的環境變量?


解決方案:

參考一: https://stackoom.com/question/2S9m
參考二: Environment variable substitution in sed
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章