TeX Live relies on the
kpse library and MacTeX is based on TeX Live.
Basically, all TeX related programs are linked to the library, whose primary job is to set up several environment variables starting from the location the program sits in.
So, for example, if you run
pdflatex and the binary resides in
/usr/local/texlive/2018/bin/x86_64-darwin/ (usually the location is determined via a soft link), the main variables that are set are (from
% SELFAUTOLOC (directory of the binary=/usr/local/texlive/YYYY/bin/i386-linux),
% SELFAUTODIR (its parent = /usr/local/texlive/YYYY/bin),
% SELFAUTOPARENT (its grandparent = /usr/local/texlive/YYYY), and
% SELFAUTOGRANDPARENT (its great-grandparent = /usr/local/texlive).
These four variables are set based on the location of the binary and used for locating the relevant
texmf.cnf files (see the documentation of the library,
texdoc kpathsea) which will set all the other necessary variables.
A program for querying the setup is
kpsewhich; among other options, it can report the value of environment variables, including those that are set at program launch by
will return, on my machine,
from which you can easily extract the year, if needed.
Just for information, when I want to test a document with different versions of TeX Live, instead of changing the distribution via the panel I just directly call the binary:
SELFAUTOLOC feature will set the environment to what results from reading the
texmf.cnf files from the 2017 distribution.