sed mongodb connection string copying multiple times in env variable through github action

this is command which i run in github action its copying into .env.production file

sed -i "s#DB_URI=.*$#DB_URI=mongodb+srv://"

And this is error/job build status which shows multiples copying


Plse let me how i can solve this

Asked By: DevOPs


Escape a few characters, and your command will work as the following:

sed -i 's#^DB_URI=.*$#DB_URI=mongodb+srv://'

Input test.txt

# comment
// comment

Output test.txt

# comment
// comment


  • The double quotes were causing bash to expand the $# to 0 or something. Stop that expansion with single quotes. Or else sed will crash with sed: -e expression #1, char 187: unterminated `s' command
  • Add a ^ start anchor to stop one kind of duplication. EDIT: This doesn’t seem to be necessary, but it’s faster and more reliable
  • Escape the & symbols to become & so that sed doesn’t expand them. According to man sed:

The replacement may contain the special character & to refer to that portion of the pattern space which matched

Answered By: Daniel T