1

I am thinking of:

function noalias() { 
  setopt localoptions noexpandalias
  "$@"
 }

But I don't know what the last line should be. eval "${*:q}"? eval "$@"? eval "${@:q}"? "${@:q}"? ${@:q}? $@? (could you explain the differences between these? )

1

Aliases are expanded at a very early stage, before a simple command is understood as such let alone a function is executed, so that can't work.

You need the noaliases (expandalias is the bash option) option to be in effect before the command line is read and parsed.

However, if you want to disable only normal aliases, that is the ones expanded in command position defined with alias foo=bar, doing:

noalias() "$@"

is enough, because if there's an alias for cmd, it wouldn't be expanded in:

noalias cmd args

Because cmd is not in command position.

That wouldn't work for global aliases though (the ones set with alias -g, like alias -g args=foo than expand any shell word whether they are in command position or not).

Even if you did:

noalias() { 
  setopt localoptions noaliases
  eval "$@"
}

And invoke your command with:

noalias 'cmd args'

That would not be foolproof, for instance after:

alias -g "'cmd arg'=gotcha"

or

alias -g noalias=gotcha

You'd need to run

set +o expandalias

Before the command is read. You could do it with a key binding though:

toggle-aliases() {
  if [[ -o aliases ]]; then
    set +o aliases
  else
    set -o aliases
  fi
  zle -I
}
zle -N toggle-aliases
bindkey '\eA' toggle-aliases
PS1='[aliases $options[aliases]] '$PS1

And press Alt+Shift+A to toggle.

  • Hmm. But noalias '...' does work if there is no malicious intent, right? What's the difference between $@ and eval "$@"? – HappyFace Nov 23 '18 at 9:41
  • 1
    @HappyFace, eval evaluates the text that's made of the concatenation of the positional parameters as shell code, "$@" runs the command whose name is stored in $1 with all the positional parameters as argument. There's no evaluation of shell code there ($@ without quotes should never be used, though it's not as bad in zsh as in other shells, it only drops the empty elements there) – Stéphane Chazelas Nov 23 '18 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.