How to use variables in a command in sed?

后端 未结 3 534
不知归路
不知归路 2020-11-22 11:28

I have abc.sh:

exec $ROOT/Subsystem/xyz.sh

On a Unix box, if I print echo $HOME then I get /HOME/COM/FILE

3条回答
  •  我在风中等你
    2020-11-22 12:10

    Say:

    sed "s|\$ROOT|${HOME}|" abc.sh
    

    Note:

    • Use double quotes so that sed would expand variables.
    • Use a separator different than / since the replacement contains /
    • Escape the $ in the pattern since you don't want to escape it.

    EDIT: In order to replace all occurrences of $ROOT, say

    sed "s|\$ROOT|${HOME}|g" abc.sh
    

提交回复
热议问题