4

I am new to the Apple "eco system" and I am trying to setup my environment variables for local development.

Here is what I have in my ~/.bash_profile (simplified for brevity):

export PATH="$JAVA_HOME/bin:$MAVEN_HOME/bin:$DEV_CLI:$PATH"
export DEV_CLI="/path/to/cli/$BRANCH_NAME/bin"
export BRANCH_NAME=dev

What I want here is to have my dev cli accessible from any point in the terminal. The crucial thing here is I want to change the BRANCH_NAME on different occasions.

Currently my solution works only if I do source ~/.bash_profile in every terminal tab/window I use.

So let's say that I open new terminal and do:

  1. echo $DEV_CLI - it will be printed,
  2. echo $PATH - the path for the cli will be missing there
  3. source ~/.bash_profile
  4. echo $PATH - all variables set in the path will be shown (cli path including)

I am running on Mac High Seirra.

Any help explanation will be deeply appreciated!

  • What is "cli. path"? You do not want an empty PATH variable at any point in your terminal. session. – fd0 Jul 11 '18 at 11:01
  • I fail to see your point, which variable you think is empty? Note that the values here are fiction ones. – Georgi Stoimenov Jul 11 '18 at 11:10
4

Your variables are out of order. You cannot set a variable based on other variables defined after it.

Set the commands in your ~/.bash_profile to the following:

export BRANCH_NAME=dev
export DEV_CLI="/path/to/cli/$BRANCH_NAME/bin"
export PATH="$JAVA_HOME/bin:$MAVEN_HOME/bin:$DEV_CLI:$PATH"

Why this is happening...

  1. When you open a new shell, it reads ~/.bash_profile
  2. It sets a PATH environment variable with other environment variables that aren't defined yet (DEV_CLI and BRANCH_NAME)
  3. The environment variables are then defined
  4. You re-source ~/.bash_profile and sets the PATH again, this time with defined variables

It's important to note that this has nothing to do with the Apple "eco system," per se. This is a Bash issue, and more generally a shell issue; you would literally have this problem whether you were on Apple, BSD, Linux, Unix and even Windows.

  • 1
    Thanks for your clarification - in the example I mistakenly switched the positions of BRANCH_NAME and DEV_CLI but still the PATH variable was on top of them anyway. – Georgi Stoimenov Jul 11 '18 at 11:09

You must log in to answer this question.

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