I'm currently working, for school, on a home-made shell, therefore I'm kind of curious nowadays about how shell vars are set. Yes, this can be really exciting !
Anyway, we obviously have to deal with the case when our shell will be launched with an empty environment, i.e. "env -i" command. With env -i, the extern variable environ is absolutely empty, so we have to build ourselves a basic PATH, PWD and everything.
Our reference is csh, cause it's easy and simple. When you launch csh with env -i and then print it's variables, here is what you can see :
$ env -i csh % set addsuffix argv () csubstnonl cwd /Users/noesierra-velasquez dirstack /Users/noesierra-velasquez echo_style bsd edit gid 20 group staff history 100 killring 30 owd path (/usr/bin /bin) prompt % prompt2 %R? prompt3 CORRECT>%R (y|n|e|a)? shell /bin/tcsh shlvl 1 status 0 tcsh 6.17.00 tty ttys001 uid 501 user noesierra-velasquez version tcsh 6.17.00 (Astron) 2009-07-10 (x86_64-apple-darwin) options wide,nls,dl,al,kan,sm,rh,color,filec % env HOSTTYPE=intel-mac VENDOR=apple OSTYPE=darwin MACHTYPE=x86_64 SHLVL=1 PWD=/Users/noesierra-velasquez LOGNAME=noesierra-velasquez USER=noesierra-velasquez GROUP=staff HOST=MacBook-Pro-de-Noe-Sierra-Velasquez.local REMOTEHOST=
Ok, so as you can see, there is no HOME set and it seems to me absolutely normal. If then you type
cd, you'll get
cd: No home directory..
We live in a normal world.
But here is the magic. Type, inside your empty csh
env -i zsh. zsh will load. Now print all env variables.
(Note : it perfectly works if you type directly
env -i zsh, even from within zsh, I do it here this way only to be perfectly sure there is no tricky trick with zsh calling himself or whatever)
% env -i zsh $ env HOME=/Users/noesierra-velasquez LOGNAME=noesierra-velasquez SHLVL=1 PWD=/Users/noesierra-velasquez OLDPWD=/Users/noesierra-velasquez PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin _=/usr/bin/env
Wow ! What happened !? zsh seems to (and did) have a way to get back $HOME and $OLDPWD from what seems to me to be the pure nothingness. (btw if anyone doesn't know, I'm pretty sure it uses paths.h to get back its path).
Obviously I know that "pure nothingness" is not a viable possibility, so I'm asking you guys : Do you know how zsh is getting back HOME and OLDPWD ?
Thank you for your patience and have a good day ! :)