Search code examples
fish

Forcing the order of arguments in function generated for a "alias"


When I create an alias like: alias "myA" "PREFIX $argv SUFFIX" fish genarates a function with body equivalent to: "PREFIX SUFFIX $argv. Is there a trick/hack/legitimate way to force the shell so that the call myA 1234 is executed as PREFIX 1234 SUFFIX?

I know how to do that by writing a function. I generate about 15+ of these, on the fly, and the PREFIX, SUFFIX strings keep changing very frequently. So it is not practical to write a function.

Thanks!

Cheers; 'best,

shankar


Solution

  • You seem to have misunderstood what the alias command does. It does define a function. But it does not do the transformation you imply. It only appears to do so because you double-quoted the alias definition. Which means that $argv is interpolated before the function is created. If, as is likely to be the case, $argv is undefined (or the empty array) then "PREFIX $argv SUFFIX" is equivalent to "PREFIX SUFFIX".

    To answer your question: No, there is no way to do what you want using the alias command. Honestly, I think alias should never have been added to fish as it isn't needed and simply confuses people migrating from bash where the concept of an "alias" is very different. It's trivial to replace

    alias myA "PREFIX $argv SUFFIX"
    

    with

    function myA; PREFIX $argv SUFFIX; end