How to use variables in a command in sed?

后端 未结 3 473
不知归路
不知归路 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:01
    This may also can help
    
    input="inputtext"
    output="outputtext"
    sed "s/$input/${output}/" inputfile > outputfile
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 12:14

    This might work for you:

    sed 's|$ROOT|'"${HOME}"'|g' abc.sh > abc.sh.1
    
    0 讨论(0)
提交回复
热议问题