How to Set a Web Env Variable From the Command Line on Linux/Unix

Image for post
Image for post

Are you a full-stack PHP developer? Chances are that you might want to easily override an environment variable through the command-line interface.

Let’s say you are writing a bash script to automate a certain process: your Docker containers must be rebuilt, and the app’s DB_HOST and REDIS_HOST variables must be updated accordingly.

On such scenario, PHP frameworks like Laravel can be easily extended in order to accomplish our goal, specifically the FlexEnv package can help like this:

However, if your PHP framework doesn’t have any extension to create, edit and delete .env files via the command line, or if you just don’t want to use an extension for managing environment variables through the command line, then the sed (stream editor) command comes to the rescue — in UNIX.

So, if your bash script needs to programmatically update the DB_HOST variable with the value of the MySQL Docker container’s gateway IP, you’d end up writing something like this:

That’s all folks! I hope you’ll find today’s tip useful.

You May Also Be Interested in…

Hi there! How are you today? I blog about technology, the Internet, SEO, programming tips, PHP and more.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store