{"id":39,"date":"2013-05-03T16:19:00","date_gmt":"2013-05-03T06:19:00","guid":{"rendered":""},"modified":"2018-05-22T21:21:56","modified_gmt":"2018-05-22T11:21:56","slug":"setting-environment-variables-in-os-x-lion","status":"publish","type":"post","link":"https:\/\/pbw.id.au\/blog\/2013\/05\/setting-environment-variables-in-os-x-lion\/","title":{"rendered":"Setting Environment Variables in OS X Lion"},"content":{"rendered":"<p>If you want to set environment variables in OS X in such a way as to be recognised in applications run from Finder, it is not enough to set the env var in <span style=\"font-family: Arial, Helvetica, sans-serif;\">.profile<\/span>. \u00a0You must also ensure that the variables are set in the file <span style=\"font-family: Arial, Helvetica, sans-serif;\">~\/.MacOSX\/environment.plist<\/span>. Setting values in that file is most conveniently done using the executable <span style=\"font-family: Arial, Helvetica, sans-serif;\">PlistBuddy<\/span>.<\/p>\n<p>On my system, the command<br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">$ which PlistBuddy<\/span><br \/>\nyields<br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"> \/usr\/sbin\/PlistBuddy<\/span><br \/>\n<em><br \/>\n<\/em><!--more-->Here&#8217;s some code you can add to <em>.profile<\/em> to set both <em>environment.plist<\/em> and the bash environment variables.<br \/>\nCreate the following functions in <em>.profile<\/em>.<\/p>\n<blockquote><p><span style=\"font-family: Arial, Helvetica, sans-serif;\">set_env_var () {<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 eval export $1=&#8221;&#8221;$2&#8243;&#8221;<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 set_plist_var &#8220;$1&#8221; &#8220;$2&#8221;<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">}<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"><br \/>\n<\/span><span style=\"font-family: Arial, Helvetica, sans-serif;\">set_plist_var () {<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"># Check current value of plist var<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 local current=`PlistBuddy -c &#8216;Print :'&#8221;$1&#8243; ~\/.MacOSX\/environment.plist`<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 if [ `expr &#8220;$current&#8221; : &#8216;Print.* Not Exist&#8217;` = &#8220;0&#8221; ]<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 then \u00a0 \u00a0 \u00a0 \u00a0# Variable exists<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 \u00a0 \u00a0 # Is the current value changing?<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 \u00a0 \u00a0 if [ X&#8221;$current&#8221; != X&#8221;$2&#8243; ]; then<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 PlistBuddy -c &#8216;Set :'&#8221;$1&#8243;&#8216; &#8220;&#8216;&#8221;$2&#8243;&#8216;&#8221;&#8216; ~\/.MacOSX\/environment.plist<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 \u00a0 \u00a0 fi<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 else \u00a0 \u00a0 \u00a0 \u00a0# Variable does not exist<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 \u00a0 \u00a0 echo $current<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 \u00a0 \u00a0 PlistBuddy -c &#8216;Add :'&#8221;$1&#8243;&#8216; string &#8220;&#8216;&#8221;$2&#8243;&#8216;&#8221;&#8216; ~\/.MacOSX\/environment.plist<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">\u00a0 \u00a0 fi<\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\">}<\/span><\/p><\/blockquote>\n<p>You may then use the function <em>set_env_var<\/em> to set both bash and <em>environment.plist<\/em> entries. For example,<\/p>\n<blockquote><p><span style=\"font-family: Arial, Helvetica, sans-serif;\">set_env_var M2_HOME &#8220;\/usr\/share\/maven&#8221;<br \/>\nset_env_var M2 &#8220;$M2_HOME\/bin&#8221;<br \/>\nset_env_var HTML_TIDY &#8220;$HOME\/.tidy&#8221;<\/span><\/p><\/blockquote>\n<div>\n<p>Note that the environment variables will immediately be available to any shell scripts or Terminal invocations, but any new variables which have been set in &lt;em&gt;~\/.MacOSX\/environment.plist&lt;\/em&gt; may not generally be available until you next login. I&#8217;m not sure at which points in a session, apart from login, &lt;em&gt;environment.plist&lt;\/em&gt; is read.<\/p>\n<p><span style=\"font-size: x-small;\">Originally published on 17 Dec 2011, and updated on 1 Mar 2012 at\u00a0<a href=\"http:\/\/pbw.livejournal.com\/8256.html\">http:\/\/pbw.livejournal.com\/8256.html<\/a>.<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you want to set environment variables in OS X in such a way as to be recognised in applications run from Finder, it is not enough to set the env var in .profile. \u00a0You must also ensure that the variables are set in the file ~\/.MacOSX\/environment.plist. Setting values in that file is most conveniently &hellip; <a href=\"https:\/\/pbw.id.au\/blog\/2013\/05\/setting-environment-variables-in-os-x-lion\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting Environment Variables in OS X Lion&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8SCfl-D","jetpack-related-posts":[{"id":34,"url":"https:\/\/pbw.id.au\/blog\/2013\/11\/setting-environment-variables-in-os-x-yosemite-and-mavericks\/","url_meta":{"origin":39,"position":0},"title":"Setting environment variables in  MacOS Big Sur","author":"pbw","date":"Thu 7th Nov '13","format":false,"excerpt":"This method uses launchctl to manage environment variables for programs invoked directly from Finder. \u00a0See the launchctl man page, especially the section LEGACY SUBCOMMANDS. \u00a0It's not entirely accurate, but that's not unusual. \u00a0The critical subcommands are getenv, setenv, and unsetenv. The man page indicates that the export subcommand is available;\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":38,"url":"https:\/\/pbw.id.au\/blog\/2013\/05\/using-environment-plist-with-mountain-lion\/","url_meta":{"origin":39,"position":1},"title":"Using environment.plist with Mountain Lion","author":"pbw","date":"Sat 4th May '13","format":false,"excerpt":"UPDATE This post is now obsolete. For the preferred method in both Mountain Lion and Mavericks, see\u00a0 Setting environment variables in OS X Mountain Lion and Mavericks. With Mountain Lion (OS X 10.8), the environment settings from ~\/.MacOSX\/environment.plist are not taken into account when the background system environment is set\u2026","rel":"","context":"In &quot;Observations&quot;","block_context":{"text":"Observations","link":"https:\/\/pbw.id.au\/blog\/category\/personal\/observations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":36,"url":"https:\/\/pbw.id.au\/blog\/2013\/05\/ant-process-elements-in-a-list\/","url_meta":{"origin":39,"position":2},"title":"Ant: process elements in a list","author":"pbw","date":"Sun 19th May '13","format":false,"excerpt":"I was looking for a way to process a list of items in an ant build file, similar to what you would do in Java with a construct like: for ( Element element : elements ) { \/\/ do stuff with element } The approach of XSLT, using recursive calls\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":31,"url":"https:\/\/pbw.id.au\/blog\/2015\/03\/help-for-digest-checking\/","url_meta":{"origin":39,"position":3},"title":"Help for digest checking","author":"pbw","date":"Fri 20th Mar '15","format":false,"excerpt":"Updated 2018-02-14 It's pretty important to check the digests of software you download. \u00a0When a downloaded file is accompanied by a signature file, for example a gnupg .asc file, you can verify the signature with various tools. \u00a0Often though, a download site will include the MD5 or SHA1 digest hash\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":30,"url":"https:\/\/pbw.id.au\/blog\/2015\/05\/tikadiff-graphical-diff-for-text-from-binary-files\/","url_meta":{"origin":39,"position":4},"title":"tikadiff: graphical diff for text from &#8220;binary&#8221; files","author":"pbw","date":"Sat 9th May '15","format":false,"excerpt":"Code The code is from the Downloads area of my Atlassian Bitbucket repository; see the README online. Version Control Systems (VCSs) VCSs like mercurial, git and bazaar (to mention only a few) are great for keeping track of changes to source files, but their utility doesn't stop there. \u00a0If you're\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":33,"url":"https:\/\/pbw.id.au\/blog\/2014\/06\/zargrep-grep-files-in-a-zip-archive\/","url_meta":{"origin":39,"position":5},"title":"zargrep: grep files in a zip archive","author":"pbw","date":"Sat 21st Jun '14","format":false,"excerpt":"How do you search for strings within a zip archive? I'm tinkering with EPUB3 files, and I wanted to be able to find certain strings within .epub files, so I had a look around, and I immediately found zgrep and family. The trouble was that zgrep assumes a single zipped\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/pbw.id.au\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":3,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions\/577"}],"wp:attachment":[{"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pbw.id.au\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}