Operating System - HP-UX
1833752 Members
2712 Online
110063 Solutions
New Discussion

What is the use of "set -u" in .profile?

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

What is the use of "set -u" in .profile?

What is the use of "set -u" parameter in the .profile login file.

Thanks.
Everyday Learning.
5 REPLIES 5
Chris Wilshaw
Honored Contributor

Re: What is the use of "set -u" in .profile?

from the sh-posix man page;

-u Treat unset parameters as an error when substituting.
Jeff Schussele
Honored Contributor

Re: What is the use of "set -u" in .profile?

Hi,

From the ksh & sh-posix man page:

-u Treat unset parameters as an error when substituting

Which means I believe that during a command substitution - EX $(command) - that any parameters that may be necessary that are unset will result in an error & not defaulted.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Todd McDaniel_1
Honored Contributor
Solution

Re: What is the use of "set -u" in .profile?

In this case "set" is not associated with an editor like EX or VI... but a command line method to either assign a env variables or set env parms...

If you have any named $VAR and they are not defined (ie have no value) they will cause an error message when your .profile is executed as you login.

A $VAR cannot have a null value for your env when invoking your .profile

This is why sometimes you will see an error for "TERM not set" when you login
Unix, the other white meat.
Bill Hassell
Honored Contributor

Re: What is the use of "set -u" in .profile?

The definitions given are correct, but why would you want to use it? From a painful experience where a spelling error deleted /usr/local/bin from 200+ machines, I ALWAYS write scripts with set -u. The shell's default behavior is to treat undefined variables as being set to null. So if you had a script like this:

for MYNAME in $LIST_OF_FILES
do
rm -rf /usr/local/bin/$MYNAM
done

If set -u was executed prior to this code, it will stop before running the rm command because $MYNAME is not = $MYNAM and therefore, $MYNAM is undefined (actually misspelled but still undefined). If set +u is in force (the default) then the script would run:

rm -rf /usr/local/bin/

and the entire directory, all files and subdirectories, would be removed (that's a bad thing!). So to prevent these mistakes, use set -u.


Bill Hassell, sysadmin
Jeroen Peereboom
Honored Contributor

Re: What is the use of "set -u" in .profile?

Bill is right: it's good programming practice to have a set -u.

I wonder however what happens if you login with CDE. and .dtprofile sources .profile and an error occurs. Are you logged in or thrown out?

JP