Bash script: Appending text at the last character of specific line of a file

前端 未结 2 1182
情书的邮戳
情书的邮戳 2021-01-15 01:13

I am trying to append a variable at the last character of a specific line of a file from a bash script.

The file is called myfile.txt and what I want to

2条回答
  •  既然无缘
    2021-01-15 01:53

    Try this:

    sed -i "/^MYVERSION=/ s/\$/$VERSION/" myfile.txt
    

    The idea is that it finds a line that starts with MYVERSION= and then replaces the end of that line with the contents of the $VERSION environment variable.

    Edit: originally I wasn't sure if the first $ needed to be escaped, but @sehe's comment and its upvoters convinced me that it does.

提交回复
热议问题