Speeding Up Your C Shell with set prompt Test

Every time you start a C shell - in a shell escape (), the su () command, a shell script, an at job (), etc.-the csh reads the cshrc file in your home directory. Some of those shells are "noninteractive," which means the shell is running a single command or reading from a script file ()- you won't be typing any commands yourself. If your cshrc has commands like alias (), set cdpath (), and others that are only useful in interactive shells, it wastes time to make noninteractive shells read them.

You can tell the shell to skip commands that will only be used in interactive shells. Set up your cshrc this way:

 if ! $? 
# COMMANDS FOR ALL C SHELLS: set path = (...whatever...) ... if (! $?prompt) goto cshrc_end # COMMANDS FOR INTERACTIVE SHELLS ONLY: alias foo bar ... set cdpath = (~ ~joe/project) cshrc_end:
Warning! The ! $?prompt succeeds only on noninteractive shells, when the shell hasn't set the prompt variable. On noninteractive shells, the command goto cshrc_end makes the shell skip to the line at the end of the file labeled cshrc_end:.

Of course, if you set your own prompt (), be sure to do it on some line below the ! $?prompt test. Otherwise, the test will always fail!

NOTE: Some tutorials tell you to use a test like this instead:

if (! $?prompt) exit # commands for interactive shells only: ...

But some C shells will log out when they see the exit command in a cshrc file. Using goto cshrc_end is more portable.

Article explains another problem that this $?prompt test solves.

- JP