sed -i .env invalid command

Ogous Chan Ali
1 min readFeb 1, 2023

It is one of the most common commands sed, as well as cp, available on Unix-like operating systems.

They both are used by daily basis while copying, inserting, replacing environment variables to CI/CD flows.

Docker containers generally runs on Linux; so you can find yourself easily if you pull a repository, or docker image on your MacOS machine, in front of an error that says:
sed: 1: ".env": invalid command

Main reason is that the behavior of shell utilities does differ in minor ways between unix variants. sed command has option -i[SUFFIX], — in-place[=SUFFIX]. It is mandatory to add on MacOS, while not on Linux. This suffix is actually declaring back-up file in which file extension should be written to, and MacOS force us to use it to.

In order to use it on MacOS, you can define a backup extension, and create a backup file with as .env.backup


sed -i “.backup” “s/SECRET_VAR=/SECRET_VAR=SOME_VALUE/” .env

Or you can use it with an empty string without creating any backup file.

sed -i “” “s/SECRET_VAR=/SECRET_VAR=SOME_VALUE/” .env

Happy coding.

--

--