setting system environment variables

jhuls
Contributor III

During the time I worked support in the Windows world I made use of system variables often. It was nothing to set a system variable to represent a particular status. This would be readable no matter what user account would be logged in.

Up to now I don't recall having a need to do this in the Mac world but now I'd like to. In delving into this thus far there doesn't seem to be anything simple to it. I'm reading about differences between OS's in how to do it and a few different commands used for it. I'm seeing set, setenv, export, and then launchctl with it's own syntax.

What am I missing besides more time to study this stuff? I would have thought this would be pretty straight forward in a unix like environment. All I need is to set a variable globally so that it can be read by any user logged into the system both before and after restart of the computer. The OS's I'm targeting for this is Yosemite, El Capitan, and Sierra. Any help would be appreciated.

5 REPLIES 5

donmontalvo
Esteemed Contributor III

What are you trying to do? There's MDM (Computer vs User), Profile Manager
, defaults, etc.

--
https://donmontalvo.com

jhuls
Contributor III

It's a bit of a long story to explain why we're doing what we're doing but on each system it was decided that we would have an environment variable that contained a 4 letter code for the dept on campus that the computer(Mac or PC) belonged to. In the past this was part of the computer naming scheme and was only 3 letters. Recent changes beyond our control dictates that it must be 4. Due to other identifiers we use in our computer names it was decided to remove this from the name since it made the name too long.

One purpose for having this is to use the 4 letter code to define what OU in AD a computer will be placed in via a script. Being at a college with dozens of departments and various room OU's under that a script eases this process. The Macs haven't been done this way in the past but we're in the process of a huge migration to a new domain and they're wanting a more standard method across the board.

The 4 letter code would also need to be referenced in inventory.

I suppose I could always simply create a hidden file tucked away with this dept code in it but a little voice in my head keeps telling me that a system variable is the way it should be done.

stevewood
Honored Contributor II
Honored Contributor II

How funny @jhuls, I just wrote up a post on how to use plist files to store data like this. You can read that at this link:

Collecting Data Using Plist Files

This would work for your need as well. Create a plist file in a directory of your choosing that contained the data, then whenever you needed the data you could read it, or, as I point out in my blog post, write an Extension Attribute that grabs that data each recon.

jhuls
Contributor III

@stevewood Thanks! I'll take a look at it. Just curious though...is there any real advantage to using plist files as opposed to unix hidden files?

stevewood
Honored Contributor II
Honored Contributor II

@jhuls the biggest advantage that I can think of is the ability to use built in calls to grab the info. Being able to use the defaults read command to read in a specific key from a plist, or the entire plist at once, makes it much easier to parse the results.