RE: recursive redefines of the PATH variable...
Why so complicated when you can just do 'export PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH:$HOME/bin'? Seems to be a very complicated means of setting a simple path?
Hi throttlemeister,
Concise reason: recursive redefines can cause trouble.
Yes, you might consider these shell commands complicated, but once written they work in all situations. For instance, sometimes you might wish to add a path to the beginning of the PATH variable, perhaps so that a different version of some utility will run instead of the standard version (say you wrote your own version of the utility which you want to run sometimes but not all the time). You can then prepend the new path, and if it is already there it won't be repeated.
Because you might have multiple shell scripts calling each other, say for init purposes, and each shell script might modify the PATH variable, you don't always know what PATH contains but you still might want to addend or prepend a new path to it. These shell commands do this. Also, say you have an initialization shell script that you use at the start of numerous other shell scripts to initialize certain variables before the work of the script is executed, and say that some of your "work" scripts can call others of your "work" scripts, then you might end up calling your initialization script many times (dozens of times, in my case). Now you could do a self-referencial $PATH redefine as you indicate, but then your PATH variable would expand with each execution of the initialization script and you would end up with a PATH with dozens of copies of the same paths all concatenated. Is this a problem? -- well, probably not so much today, but when this command was written it was a concern because the PATH variable had a relatively small finite size on the particular OS for which it was originally written and thus you didn't want to overrun the variable length causing an error. It is just good programming practice.
Now you could write shell commands into your initialization script so that it is only executed once in a single process and its subprocesses even though it is called multiple times, but this is essentially all that the prepend and append commands are doing, in the particular case of the PATH variable. This is actually the reason why these commands were written in the first place, and many years ago I gave them to friends who gave them to friends who gave them to friends and after about a year someone gave them back to me not realizing from where they had come. And there were a few minor modifications along the way, but this type of shell command strategy has ended up in all kinds of places, especially OS init routines, the GNU Project, Linux distros, books, tutorials, etc.
...sorry for the longwinded explanation...in a nutshell, self-referencial redefines can lead to problems...
Regards,
Switon