“user_data” script can't run a sed command

I am creating a DigitalOcean droplet through their API. I am using their “user_data” feature to run commands upon creation.

This issue is that the “sed” command doesn’t work for this, and I can’t understand why. It works perfectly fine if you enter the command through the command line, but not in the script.

The code below is what I am using. Don’t mind the \' I am using PHP so I have to include those.

Anyways, I know that the script is running because the “installed” file is being created successfully. It’s just that the sed command isn’t running for some reason.

'user_data' => '                    
- sed \'/DB_DATABASE/s/=.*/=test/g\' /var/www/html/app/.env
- sed \'/DB_USERNAME/s/=.*/=test/g\' /var/www/html/app/.env
- sed \'/DB_PASSWORD/s/=.*/=test/g\' /var/www/html/app/.env
- touch /var/www/html/app/storage/installed

The expected outcome is for the sed commands to work and change the values accordingly. The actual result is that the command isn’t ran, or just doesn’t work.

If you need more information please let me know!

Thank you


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hello, @Rainn

Looking at this I can see that you’re not using any options for the sed command itself. Usually the -i option should change the data for you, so it will be sed -i

Let me know how this goes.