What's the Difference Between "Env" and "Set" (On MAC Os X or Linux)

What's the difference between env and set (on Mac OS X or Linux)?

Long story short: set can see shell-local variables, env cannot.

Shells can have variables of 2 types: locals, which are only accessible from the current shell, and (exported) environment variables, which are passed on to every executed program.

Since set is a built-in shell command, it also sees shell-local variables (including shell functions). env on the other hand is an independent executable; it only sees the variables that the shell passes to it, or environment variables.

When you type a line like a=1 then a local variable is created (unless it already existed in the environment). Environment variables are created with export a=1

Environment variables in Mac OS X

There's no need for duplication. You can set environment variables used by launchd (and child processes, i.e. anything you start from Spotlight) using launchctl setenv.

For example, if you want to mirror your current path in launchd after setting it up in .bashrc or wherever:

PATH=whatever:you:want
launchctl setenv PATH $PATH

Environment variables are not automatically updated in running applications. You will need to relaunch applications to get the updated environment variables (although you can just set variables in your shell, e.g. PATH=whatever:you:want; there's no need to relaunch the terminal).

How is the PATH env variable set when opening a BASH shell in Terminal.app on OS X?

I've found the culprit. The secret sauce was /usr/libexec/path_helper it looks in the file /etc/paths and in the directory /etc/paths.d/.

First bash sources /etc/profile which executes the following code:

if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
# The above line is the secret sauce, so to say...
# First is adds default PATH values from the file /etc/paths
# Then all files in the /etc/paths.d/ directory are read and directories listed
# in each file (one per line) are appended to PATH
fi

if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi

Next bash looks for ~/.bash_profile, ~/.bash_login, and ~/.profile.

Listing these steps out, PATH is built as follows:

  1. Directories in the file /etc/paths are added to PATH
  2. Directories listed in the files in the directory /etc/paths.d/ are appended to PATH — Note, that these are appended versus being prepended.
  3. Various PATH={DIR_2_ADD}:"${PATH}" statements in my ~/.bash_profile and ~/.bashrc files are prepend PATH

Set environment variables on Mac OS X Lion

First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:

~/.bash_profile

Update for Mavericks

OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.

Lion and Mountain Lion Only

OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:

~/.MacOSX/environment.plist

If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.

Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).

It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.

One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.

Edit: Here are some instructions for creating a plist file.

  1. Open Xcode
  2. Select File -> New -> New File...
  3. Under Mac OS X select Resources
  4. Choose a plist file
  5. Follow the rest of the prompts

To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.

Once the plist file is created you can open it with Xcode to modify it anytime you wish.

Mac OS X 10.9 - setting permanent environment variables

Drop the $(...) bit, which would attempt to execute the command within the brackets and set $MULE_HOME to whatever it produces. In your case /opt/mule-standalone-3.4.0 is not an executable, hence the error you are getting.

export MULE_HOME=/opt/mule-standalone-3.4.0

and use ~/.bashrc not ~/.bash_profile.

EDIT: It seems opinion is that you should set environment variables in your ~/.bash_profile script, and not ~/.bashrc script.

In layman's terms, what is the difference between Mac OS, Ubuntu, Linux, and Unix?

Unix is an operating system originally developed in the 1970s, on which Mac OS X is based. Linux is a kernel (the part of an operating system which interfaces with the hardware), while distributions such as Ubuntu or Fedora add the rest of the software (much of which comes from the GNU project) to make it into a full OS. Linux and the GNU project originally came to exist to provide a free alternative to the closed-source Unix, so while the code is not descended from Unix, they are quite similar.

Setting PATH environment variable in OSX permanently

You have to add it to /etc/paths.

Reference (which works for me) : Here

Set environment variables from file of key/value pairs

Problem with your approach is the export in the while loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).

Add export command in the file itself:

export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"

Then you need to source in the file in current shell using:

. ./conf/prac1

OR

source ./conf/prac1


Related Topics



Leave a reply



Submit