1

It is known that attempting to invoke a custom alias inside itself (be it directly; or indirectly via intermediate aliases that call each other) would cause an "Alias loop." error, and rightfully so.

However, I would like to write a recursive csh alias, as I intend to include a base case to the recursion, so in my use case it is not an infinite loop. Here's a simplified example:

alias countdown 'if ( \!* > 0 ) then; echo \!*; countdown `expr \!* - 1`; endif;'

The expected output from this alias, for example with input 3, should be:

$ countdown 3
3
2
1
0

Attempting to define such an alias leads to the aforementioned "Alias loop." error on csh. How can I achieve this functionality correctly? I tried using a while loop in the alias instead of recursion, but it doesn't seem to be supported in csh, and other workarounds for this alias loop issue are not applicable since I do not have a built-in counterpart to invoke.

7
  • Does it have to be an alias? I know nothing about csh, but it would be really weird if an alias could reference itself like that. It would make it impossible to do basic things like aliasing ls to ls -l. Can't you use a function or a little script instead? Commented May 11 at 13:19
  • @terdon original CSH doesn't have functions (you can emulate badly, but you can, emulate them using aliases. The hoops you need to jump are pretty annoying. It's a pretty big irony to me that csh, which was supposed to be "more like C" than the original Bourne shell, doesn't carry functions as syntax elements. That's just such a stark oversight, imho.) Commented May 11 at 13:40
  • Ah, OK. So functions are out, thanks @MarcusMüller. But is it even possible with aliases? Allowing such recursion seems like it would render aliases useless since you wouldn't be able to do things like alias some_command='some_command --some-flag' which is a very basic use of aliases. So I guess a little script is the best way? (or just not using csh, of course :P) Commented May 11 at 14:14
  • Well it's been long advised not to use csh for anything involving "complex" scripting, but sometimes you have to make do with what you have... To answer your point though, you can actually do aliases like alias ls "ls -l", and why that works is explained in the accepted answer in the question I linked above Commented May 11 at 16:06
  • I see, thanks! Still, that suggests what you want isn't possible unless you can force your command to be the first word. This really sounds like it would be better to have a tiny script in your PATH that does this instead 9f an alias. Commented May 11 at 16:49

1 Answer 1

0

I've found a funny workaround, which may shed some light on how csh's Alias loop. detection mechanism works... The solution for my made-up scenario above is to insert an eval command before the recursive alias invocation.

alias countdown 'if ( \!* > 0 ) then; echo \!*; eval countdown `expr \!* - 1`; endif;'
#                                               ^^^^ 

It seems that, this way, csh does not consider the alias to be calling itself (since we're not invoking the alias directly as the LHS of a statement). So perhaps it isn't just always checking whether the alias simply exists in its own definition or not, it specifically checks whether it is invoked.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.