2

I have a couple of questions:

  1. On PC I can get the username from the environment variable USERNAME and on LINUX it is USER - what the MAC equivalent?

  2. The users local area on windows can be found via the registry (or equivalent) , on LINUX you type ~ at the command prompt - what is the MAC equivalent?

Note I would like to extract these from environment variables if possible (from code).

e.g. I want to create a file in the users home area on MAC > how would I build the following variable -:

This is what I think it is - can anyone confirm (note I dont currently have a MAC in front of me to test it...)

% e.g.
userName = getenv ( 'USER' );  
userLocalArea = '~/Library/Preferences/';
filename = fullfile ( userLocalArea, userName );

In the end the language I am doing this in is Matlab - but its a pretty general question...

3

You don't actually need the user name to put something in their home structure.

Simply starting ~/ will direct it to their home, so ~/Library/ will go to the current user's
[Boot Drive]/Users/[User Name]/Library/.

If you need to install for all users, use /Library/ instead.

I guess it's the equivalent of Windows' %appdata%/

If you really need the name & folder …

userName=$(logname)   
    or userName=$(user)
userLocalArea=$(home)

After tests, there seems to be some inconsistency - these work better in Terminal… as some calls don't appear to work in both upper & lower case, it looks like upper case is a safer 'rule'. Someone smarter than me will have to fill in why some work in brackets & some don't.

userName=$LOGNAME
    or userName=$USER
userLocalArea=$HOME

That will get their shortname & home folder

Not sure how to get longname without osascript…

osascript -e "long user name of (system info)"

This all assumes your install is being done by the currently logged user.

3
  • Hi thanks - I want to extract the [USER NAME] for other reasons than obtaining the home structure. If I follow it is the environment variable `logname' or 'user' (is it case sensitive?) – matlabgui Apr 29 '15 at 8:06
  • It's not normally something I play in, so just doing some tests which seem a bit confusing to me, though idk the underlying reason… echo $(logname) works upper or lower; echo $(user) doesn't work at all, neither does echo $logname but echo $LOGNAME does; echo $user works either case; echo $(home) or echo$(HOME) doesn't work, echo $home doesn't, yet echo $HOME does work. – Tetsujin Apr 29 '15 at 8:18
  • I tweaked the answer - I really am not sure why one syntax works for one call yet not another. – Tetsujin Apr 29 '15 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.