leahneukirchen/nq

Shell-agnostic way to run nested aliases through `nq` (at least `bash` and `zsh`)?

xanoni opened this issue · 5 comments

What is the best practice way to have nq some-alias auto-resolve the alias, ideally even when some-alias is a nested alias?

And what if it is a function or a function inside an alias?

This seems to work:

alias nqs="nq "\$SHELL" -ic"

Way to go?

With bash and zsh you can define alias nq='nq ' and it will expand the aliases.

From bash man page:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

And from zsh:

A trailing space in value causes the next word to be checked for alias expansion.

Even dash supports it and its part of the posix shell spec:

If the value of the alias replacing the word ends in a <blank>, the shell shall check the next command word for alias substitution; this process shall continue until a word is found that is not a valid alias or an alias value does not end in a <blank>.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03_01

So this should be fairly shell agnostic as long as they somewhat follow posix specifications on aliases.

Rigghhttt... there as something. Let me see how it compares. The less elegant solution I proposed above already seems to be working well.

Hmmm ... in my zsh v5.8 your proposed solution doesn't work. It only expands the first level of aliases in my tests. Am I screwing up, or can this only expand one level?

What @Duncaen says is correct, but only moderately useful, as an alias that expands to something more complex than a simple command will not work.

I guess calling the shell with -i to enable alias expansion is the way to go.

Alright. And it's also generic and has worked without trouble so far. I'll close this.