18

I want to set an environment variable using setenv:

setenv NODE_ENV localhost  

But setenv gives me an error saying the command is not found. Does anyone know how to perform the MAC OSX equivalent of setenv? Thanks!

Howiecamp
  • 2,981
  • 6
  • 38
  • 59
user1871869
  • 3,317
  • 13
  • 56
  • 106
  • How is node.js relevant to your question? – Keith Thompson Aug 26 '14 at 16:57
  • @KeithThompson - The question is obviously not about node (of course he is doing this to get node working but the answer to the question is independent of node); the OP is looking to set an environment variable and node is just the example. I am going to edit the question. – Howiecamp Jan 12 '15 at 20:57
  • @Howiecamp: The OP used the "node.js" tag. The question certainly doesn't look like it's really about node.js, but as far as I knew there could have been some connection that I was missing. The *expected* answer is that it's not relevant, but I was hoping that the OP would respond. – Keith Thompson Jan 12 '15 at 21:09

3 Answers3

35

you want export

NODE_ENV=localhost
export NODE_ENV

or on 1 line export NODE_ENV=localhost

and this has nothing to do with OSX per se, more to do with bash vs (t)csh as your shell

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
Doon
  • 19,719
  • 3
  • 40
  • 44
8

Best for Mac is:

launchctl setenv NODE_ENV localhost

if you want to make the variable persistent and avoid rebooting.

See:

http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-mac-os-x-slash-etc-slash-launchd-dot-conf/

German
  • 10,263
  • 4
  • 40
  • 56
  • How is `launchctl` relevant? – Keith Thompson Aug 26 '14 at 14:09
  • So you don't have to reboot. See: http://stackoverflow.com/questions/2519292/setenv-variable-with-spaces-in-launchd-conf Are you a Mac owner? It's common practice – German Aug 26 '14 at 14:53
  • More references: http://codepulsive.blogspot.com.es/2013/11/setting-environment-variables-in-os-x.html http://apple.stackexchange.com/questions/51677/how-to-set-path-for-finder-launched-applications – German Aug 26 '14 at 15:31
  • Hmm. I took the question to be about [t]csh vs. bash (the authors of the other answers made the same assumption), but apparently it also has something to do with node.js, which I'm not familiar with. Still, I think the `setenv` command the OP was asking about is the builtin command in [t]csh. `export NODE_ENV=localhost` in bash, is exactly equivalent to `setenv NODE_ENV localhost` in [t]csh. The OP doesn't appear to have been asking how to make the setting persistent; that would be a separate question. Is there some obvious connection between node.js and `launchctl`? – Keith Thompson Aug 26 '14 at 16:07
  • Ok fair enough, but since the author does not mention shell differences I did not realize that. No, there's no connection whatsoever between node.js and launchctl – German Aug 26 '14 at 16:56
  • 2
    @KeithThompson - I am not sure why you keep pushing this question of relevancy and about the relationship to node. I suspect you know there is no obvious connection between node.js and launchctl. Node js happened to be the example only. The variable could have been called NOT_NODE; it's just a name. – Howiecamp Jan 12 '15 at 21:02
0

Are you a Cshell person?

The earlier versions of OS X came with tcsh as the default shell since OS X is based upon BSD. However, Mac OS X comes with BASH as the user's default shell.

Macs still come with Turbo Csh too and you can make this your default shell if that is your desire. In the terminal, type:

$ chsh -s /bin/tcsh

If you decide you want switch back to BASH:

$ chsh -s /bin/bash

You can see a list of all possible shells in the /etc/shell file.

David W.
  • 105,218
  • 39
  • 216
  • 337