0

Is there a way to run all commands in current directory with some ENV variables predefined?

Example of what I need:

I put some file, say .env in my directory, with:

export SECRET='abcd123'

Then I run some command, say rails c, but in fact it should run as SECRET='abcd123' rails c. Of course, when I run command from another directory, this ENV variable should not be used.

1 Answer 1

0

In your home directory edit .bashrc and add the following function:

function dosecret()
{

    [ -f ./.env ] && source ./.env
    $*
    unset SECRET
}

To test run source .bashrc.

If there is a .env file in the current directory it is read (where SECRET variable is defined). The command is then run ($*) and after the command is complete the SECRET variable is unset.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .