How to source environment variable in command line with && combiner?

For a NodeJs app, I wrote the following script to deploy the app:

"scripts": {
  "deploy": "source private/create_env.sh && ./deploy.sh",
}

private/create_env.sh creates environment variables that are used in the deploy.sh script.

export USERNAME=alphonsio
export SERVER=ftp.example.com
export ...

The && combiner prevent the environment variable from being available in the deploy.sh script.

How to make them available in the deploy script?

Asked By: Alphonsio

||

The && combiner prevent the environment variable from being available in the deploy.sh script.

No it doesn’t.

If I have create_env.sh like this:

#!/bin/sh
export SOME_CUSTOM_VAR=foo

And deploy.sh like this:

#!/bin/sh

echo "Value is: $SOME_CUSTOM_VAR"

Then running:

source create_env.sh && ./deploy.sh

Produces:

Value is: foo
Answered By: larsks

I believe your problem has nothing to do with &&:

/bin/sh is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash for sh, it supports source.

On Ubuntu, though, /bin/dash is used which does not support source. Most shells use . instead of source. If you are on one of these systems, replace source by . in your package.json file:

"scripts": {
  "deploy": ". private/create_env.sh && ./deploy.sh",
}
Answered By: FIfi