How to unset env variable on OS X?

WilliamKF picture WilliamKF · Mar 25, 2014 · Viewed 13.8k times · Source

I'm working on a makefile rule and wish to unset the environment variable MAKEFILES before invoking make again recursively. On other BSD systems, I do it like this:

env -u MAKEFLAGS $(MAKE) $(SUBDIR_ARGS)

On Linux, I do it like this:

env --unset=MAKEFLAGS $(MAKE) $(SUBDIR_ARGS)

However, neither style is working on Macintosh OS X Mavericks, although, I would expect the BSD style used elsewhere to work.

The error I get with the BSD style invocation is:

env: illegal option -- u

How can I unset an environment variable before invoking a command on OS X and why doesn't the BSD style used elsewhere work?

Here is Mac's man page for env:

ENV(1)                    BSD General Commands Manual                   ENV(1)

NAME
     env -- set and print environment

SYNOPSIS
     env [-i] [name=value ...] [utility [argument ...]]

DESCRIPTION
     env executes utility after modifying the environment as specified on the command line.  The option name=value specifies an environmental variable, name, with a value of value.  The option `-i' causes env to completely ignore the environment it inherits.

     If no utility is specified, env prints out the names and values of the variables in the environment, with one name=value pair per line.

DIAGNOSTICS
     If the utility is invoked, the exit status of env shall be the exit status of utility; otherwise, the env utility exits with one of the following values:

     0       The env utility completed successfully

     1-125   An error occurred in the env utility.

     126     The utility specified by utility was found, but could not be invoked.

     127     The utility specified by utility could not be found.

COMPATIBILITY
     The historic - option has been deprecated but is still supported in this implementation.

SEE ALSO
     execvp(3), environ(7)

STANDARDS
     The env utility conforms to IEEE Std 1003.2-1992 (``POSIX.2'').

BUGS
     env doesn't handle commands with equal (``='') signs in their names, for obvious reasons.

BSD                             August 27, 1993                            BSD

Answer

SevenBits picture SevenBits · Mar 25, 2014

Use the unset command. From the man pages:

unset

Remove variable or function names - the opposite of set.

Syntax
  unset [-fv] [name ...]    

  Key
   -v   Each name refers to a shell variable.(default)

   -f   Each name refers to a shell function, and the function definition is removed. 

  Example

$ unset myvariable

Each unset variable or function is removed from the environment passed to subsequent commands. If any of RANDOM, SECONDS, LINENO, HISTCMD, FUNCNAME, GROUPS, or DIRSTACK are unset, they lose their special properties, even if they are subsequently reset.

The exit status is true unless a name is readonly. It is not an error for nothing to be unset.

unset is a bash built in command.

"Look Dave, I can see you're really upset about this" ~ HAL (Space 2001)