How to add environment variables in Mac OS X

Adding here for reference (tried this in Mac OS X Mojave):


# To set an environment variable, enter the following command:
launchctl setenv variable "value"

# To find out if an environment variable is set, use the following command:
launchctl getenv variable

# To clear an environment variable, use the following command:
launchctl unsetenv variable

My .bash_profile

Don’t know why I never added it here, so doing it now.


alias ll="ls -al"
alias src="cd ~/workspace/"
alias ap="cd ~/workspace/git/antonperez/"

alias readyapi=”/Applications/ReadyAPI-2.5.0.app/Contents/java/app/bin/ready-api.sh”

performGitOperations() {
git fetch –all
git checkout $1
git reset –hard origin/$1
git clean -fd
}

copom() {
echo “[———- START ———-]”
if [ “$2” = “all” ]; then
echo “–>> blwebdriver <<–” cd ~/workspace/git/qa/blwebdriver performGitOperations $1 echo “–>> blutilities <<–” cd ~/workspace/git/qa/blutilities performGitOperations $1 fi echo “–>> blpages <<–” cd ~/workspace/git/qa/blpages performGitOperations $1 echo “–>> blcucumber <<–”
cd ~/workspace/git/qa/blcucumber
performGitOperations $1
echo “[———- DONE ———-]”
}

 

git-all() {
git fetch –all
git checkout $1
git reset –hard origin/$1
git clean -fd
}

if [ -f $(brew –prefix)/etc/bash_completion ]; then
. $(brew –prefix)/etc/bash_completion
fi

gitPS1(){
gitps1=$(git branch 2>/dev/null | grep ‘*’)
gitps1=”${gitps1:+ (${gitps1/#\* /})}”
echo “$gitps1”
}

PS1=’\u@\h:\w$(gitPS1)$ ‘

Install sqlcmd on Mac OS X

In case you need to run SQL Server scripts from your Mac, do the following to install (NOTE: I use HomeBrew.):

    • From the terminal, type:

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
ACCEPT_EULA=y brew install --no-sandbox msodbcsql mssql-tools

    • You should see something like in the screenshot. Type “YES” if prompted and be on your way.
    • Once done, type:

sqlcmd

OS X: Setting system-wide environment variables

I believe up until Yosemite, I didn’t have issues setting up environmental variables (i.e. JAVA_HOME) via my .bash_profile. As I upgraded, that no longer worked, so I found another way: use a custom .plist in your LaunchAgents folder:

  1. Go to ~/Library/LaunchAgents
  2. Add something like the below and then reboot, or unload-load it via launchctl unload
  3. That’s it!
  4. 
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
      <key>Label</key>
      <string>my.startup</string>
      <key>ProgramArguments</key>
      <array>
        <string>sh</string>
        <string>-c</string>
        <string>launchctl setenv APPS_CONFIG '/Users/anton/box/Source/apps_config' | launchctl setenv CATALINA_BASE '/usr/local/Cellar/tomcat7/7.0.63/libexec' | launchctl setenv CATALINA_HOME $CATALINA_BASE | launchctl setenv HOMEBREW_GITHUB_API_TOKEN 'b20aeb90b4d0cbc8352bdc6059073b7be473e4e3' | launchctl setenv JAVA_HOME '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/' | launchctl setenv M2_HOME $'/usr/local/Cellar/maven@3.2/3.2.5/libexec' | launchctl setenv GATLING_HOME '/usr/local/Cellar/gatling/2.1.7/'</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
    </dict>
    </plist>