UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 10.5 Sourceable Scripts Chapter 10
Aliases
Next: 10.7 How to Put if-then-else in a C Shell Alias
 

10.6 Avoiding C Shell Alias Loops

[Article 8.11 has similar information for bash. -JP]

Here's a situation that came up on the Net a while ago. Someone wanted an exit (38.4) alias that would run a ~/.exit file (14.14) before leaving the shell. The obvious solution is:

alias exit "source ~/.exit; exit"

This doesn't work; when you use the exit alias, the C shell thinks that the alias is trying to execute itself. Recursive aliases aren't allowed on many shells, so the C shell prints an error message (Alias loop) and gives up.

There are many, many ways to break the loop. Here's the best (in my opinion):

alias exit 'source ~/.exit; ""exit'

Article 8.12 has the hairy details of what works and why. To summarize, if you need to use the alias's name within a C shell alias, you can use:
""name

where name is the name of a built-in (1.10) command or any "regular" command.

\name

where name is the name of any "regular" command, but not a built-in command.

Tempting as this all may sound (and I have to admit, if it didn't sound a bit tempting, I wouldn't be writing this article), I can't really recommend the practice of "redefining" commands with aliases. You should leave the original commands as they are. The original author could have avoided all these problems by calling his alias quit rather than exit.

If you redefine commands with aliases - then use another account where your alias isn't defined (or, if you let someone type a command on your account)-it's easy for things to go wrong. That's especially true for commands that do something permanent - overwriting or removing files, for example.

Let me give one more example to show you what problems you can have. Let's say you've aliased the exit command to source a .exit file before quitting. Fair enough. But now, let's say that you're not in your login shell, that you've set ignoreeof (6.9), and that, for no apparent reason, your .exit file disappears (maybe it develops a bad block, so the system can't read it; such things happen).

Now you're stuck. If you type exit, the source command will fail, and the "real" exit command will never be executed. You can't leave the shell. Of course, if you remember what you did, you can always type unalias exit and get the original command back. Or you can type <"><">exit. But if you've foisted this alias on a beginner, he or she might not know that. All of a sudden, you're stuck in some shell that you apparently can't get out of.

The biggest virtue of UNIX is that it's infinitely extendable. However, you aren't helping if your extensions hide the basic operations that make everything work. So - extend all you want. But when you write your extensions, give them new names. End of sermon.

- ML ML wrote, but material came from net postings by several people


Previous: 10.5 Sourceable Scripts UNIX Power ToolsNext: 10.7 How to Put if-then-else in a C Shell Alias
10.5 Sourceable Scripts Book Index10.7 How to Put if-then-else in a C Shell Alias

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System