Running Shell Script Using .Env File

Running shell script using .env file

You need to source the environment in the calling shell before starting the script:

source 'filename.env' && bash 'scriptname.sh'

In order to prevent polution of the environment of the calling shell you might run that in a sub shell:

(source 'filename.env' && bash 'scriptname.sh')

using .env property in bash script

You could source the .env file. As the format KEY=value is compatible with how bash does its environment variables. So in your case, start.sh would be

#!/bin/bash
source .env

pm2 delete echo $NODE_ENV
pm2 start "npm run build && npm run start:prod" --name $NODE_ENV --log-date-format 'DD-MM HH:mm:ss.SSS'
pm2 logs

Set environment variables from file of key/value pairs

Problem with your approach is the export in the while loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).

Add export command in the file itself:

export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"

Then you need to source in the file in current shell using:

. ./conf/prac1

OR

source ./conf/prac1

How to set environment variables from .env file


If your lines are valid, trusted shell but for the export command

This requires appropriate shell quoting. It's thus appropriate if you would have a line like foo='bar baz', but not if that same line would be written foo=bar baz

set -a # automatically export all variables
source .env
set +a


If your lines are not valid shell

The below reads key/value pairs, and does not expect or honor shell quoting.

while IFS== read -r key value; do
printf -v "$key" %s "$value" && export "$key"
done <.env

How Can I Execute Environment Variables in Shell Script?

Actually, you need to transform $i into the name of the variable, then read $varname. If the shell would support this, you should write cd $$i. Unfortunately, this will not work, because $$ gives the current PID.

As suggested by @Biffen, you should use shell variable substitution:

cd ${!i}

Previous answer, using dangerous eval instruction:

eval cd \$$i

Note: eval is a dangerous instruction. Use it only if you are sure of the content of your files (not files provided by untrusted users).

How to temporarily load an env file for a single shell command, as a bash util/function/alias?

Based on your xenv():

$ xenv() { (set -a && source "$1" && shift && "$@"); }
$ cat envfile
FOO='hello world'
$ FOO='old value'
$ echo $FOO
old value
$ xenv ./envfile bash -c 'echo $FOO'
hello world
$ echo $FOO
old value
$

Note that usage like xenv ./envfile echo "$FOO" does not work because $FOO would expand in current shell's env.

How can I load environment variables from another file in my makefile?

Every recipe line in a makefile is run in its own shell. Environment variables modify the current shell, but those changes go away when the shell exits. So, it's completely impossible for one recipe to set environment variables that are then visible inside a different recipe.

If you want these variables available you must source them in each recipe line, like this:

diff-dev:
. ./config/dev-env && cdk diff

deploy-dev:
. ./config/dev-env && cdk deploy

You can put this into a variable, like:

CDK = . ./config/dev-env && cdk

diff-dev:
$(CDK) diff

deploy-dev:
$(CDK) deploy

Alternatively if your dev-env file is a simple-enough format (such as the one you show) that it works as both a makefile AND a shell script, you could include it:

include ./config/dev-env

diff-dev:
cdk diff

deploy-dev:
cdk deploy

But this will only work for very limited contents of dev-env (basically simple assignment of variables to static strings).



Related Topics



Leave a reply



Submit