6

I'm trying to create a Shell Script to automate my local dev environment. I need it start some processes (Redis, MongoDB, etc.), set the environment variables then start the local web server. I'm working on OS X El Capitan.

Everything is working so far, except the environment variables. Here is the script:

#!/bin/bash

# Starting the Redis Server
if pgrep "redis-server" > /dev/null
then
    printf "Redis is already running.\n"
else
    brew services start redis
fi

# Starting the Mongo Service
if pgrep "mongod" > /dev/null
then
    printf "MongoDB is already running.\n"
else
    brew services start mongodb
fi

# Starting the API Server
printf "\nStarting API Server...\n"
source path-to-file.env
pm2 start path-to-server.js --name="api" --watch --silent

# Starting the Auth Server
printf "\nStarting Auth Server...\n"
source path-to-file.env
pm2 start path-to-server.js --name="auth" --watch --silent

# Starting the Client Server
printf "\nStarting Local Client...\n"
source path-to-file.env
pm2 start path-to-server.js --name="client" --watch --silent

The .env file is using the format export VARIABLE="value"

The environment variables are just not being set at all. But, if I run the exact command source path-to-file.env before running the script then it works. I'm wondering why the command would work independently but not inside the shell script.

Any help would be appreciated.

20
  • What exactly is in that file? How does echo $- compare between your interactive shell where it works and the script where it doesn't? Commented Aug 19, 2016 at 17:24
  • ...the most likely scenario is that you're running set -a in ~/.bashrc or similar. Commented Aug 19, 2016 at 17:24
  • (assuming reasonable expectations -- ie. that you don't expect the variables to be interactively available after the script exits). Commented Aug 19, 2016 at 17:26
  • ...btw, inspecting output of brew services list is a better choice than pgrep here -- you don't want a separate script called, say, poll-mongod to match pgrep mongod. Commented Aug 19, 2016 at 17:31
  • ...BTW, "I'm using echo $- in the shell to check" doesn't actually tell me anything useful. The output of $- doesn't reflect which variables are established, but which (of a specific subset of) shell configuration settings are active. Please provide the exact values given by both instances. Commented Aug 19, 2016 at 17:33

1 Answer 1

18

When you execute a script, it executes in a subshell, and its environment settings are lost when the subshell exits. If you want to configure your interactive shell from a script, you must source the script in your interactive shell.

$ source start-local.sh

Now the environment should appear in your interactive shell. If you want that environment to be inherited by subshells, you must also export any variables that will be required. So, for instance, in path-to-file.env, you'd want lines like:

export MY_IMPORTANT_PATH_VAR="/example/blah"
11
  • The OP is sourcing the file. I presume that they're checking whether the variables are set via the behavior of their pm2 services invoked afterwards; if they intend them to be available in their outer shell, then yes, that's an obvious expectations failure. Commented Aug 19, 2016 at 17:25
  • I'm using echo $- in the shell to check and they're not being set. This does make sense though. Is there any way to do what I want to do without sourcing each file individually?
    – Nick Corin
    Commented Aug 19, 2016 at 17:27
  • 1
    I see source invocations within the script. There's no indication that the script itself has been sourced from the interactive shell.
    – Juan Tomas
    Commented Aug 19, 2016 at 17:28
  • I was running the script above with ./start-local.sh .. I'm very new to shell scripting so I wasn't aware that it ran in a subshell.
    – Nick Corin
    Commented Aug 19, 2016 at 17:29
  • @nickcorin, waitaminute. Which shell do you expect these variables to be set in? The one that's executing your script, or the one from which you're interactively invoking that script? I asked you to echo $- to see if its output included the letter a (which would mean that all assignments are automatically exported). Commented Aug 19, 2016 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.