Run a command that is shadowed by an alias

Let’s say I have the following alias in bash – alias ls='ls --color=auto' – and I want to call ordinary ls without options. Is the only way to do that is to unalias, do the command and then alias again? Or there is some nifty trick or workaround?

The alias is just a defined shortcut. In this example, the alias defined is the string ls, which executes ls --color=auto. If you don’t want that behavior, you can call the binary ls using the absolute path.

So executing just /bin/ls will produce output without color because it is not the alias you defined.

You could also change the alias to something different, or add a new alias that executes just ls without the color parameters.

Answered By: George M

You can also prefix a back slash to disable the alias: ls

Edit: Other ways of doing the same include:

Use “command”: command ls as per Mikel.

Use the full path: /bin/ls as per uther.

Quote the command: "ls" or 'ls' as per Mikel comment.

You can remove the alias temporarily for that terminal session with unalias command_name.

Answered By: Not Now

That’s what the command command is for.

Try

command ls

This tells the shell to bypass aliases and functions.

This way is supported by bash, zsh, and ash/dash.

Answered By: Mikel

Typing the command in uppercase seems to work, like LS, though I’m not really sure why.

Answered By: asmeurer

Another way, building upon @uther’s answer, is to use $(which ...) or `which ...` (using backticks ``) to automatically find the executable’s path. This will work in bash:

$(which ls)

`which ls`

Undoubtedly, simply prefixing the command with a is much shorter.

UPDATE: According to How to use `which` on an aliased command?, this doesn’t seem to be reliable at all.

Answered By: krlmlr

Personally, I just avoid defining aliases with the same names as commands, so ls always invokes the actual command. I have aliases or functions for various combinations of options like l, ll, and lg. (This means I need unalias ls in my .bashrc or .cshrc.)

I find that the OS’s assumptions about which options I might prefer (overriding the assumptions of the designers of the ls command itself) rarely match my own personal tastes. I happen to dislike the look of ls --color=auto, and its legibility can vary greatly between black-on-white and white-on-black.

YMMV, and the other solutions are of course still good to know.

Answered By: Keith Thompson

If ls and command ls are "not working", use type -a ls to troubleshoot what is happening.

In my case, ls and command ls were returning nothing, and I was going crazy trying to figure out why. Turns out I somehow had a zero-byte file named ls in one of my paths, ~/bin.

I was able to fix the situation by deleting ~/bin/ls. Then hash -d ls to remove it from the hash table.

Side note:

which -a ls was not helpful because it only returned /bin/ls. Apparently, which cannot interpret the tilde in my path: ~/bin. However type -a ls can.

Answered By: wisbucky

For zsh

Order:

  1. you type in the shell (stdin)
  2. history expansion, such as !!, !-3
  3. alias expansion (try ctrlx then a, to see the expanded result)
  4. other expansions (order: Process Substitution > Parameter Expansion > Command Substitution > Arithmetic Expansion > Brace Expansion)

suppose we have:

alias pwd='echo "An Alias"'

then this function definition will get the error zsh: defining function based on alias 'pwd':

pwd(){ 
    echo "A function"
}
  • alias expansion may be avoided by adding any kind of quoting to the word. If you want to use unaliased pwd, type 'pwd', pwd or "pwd" (`pwd` is not regarded as quoting)
  • builtin pwd use zsh’ s built-in function(aka command?) pwd
  • command pwd use /bin/pwd, (from GNU)

If there is no alias pwd="echo WhatEver_blabla", and this is in zshrc:

pwd(){ 
    echo "A function"
}

then quoting like 'pwd' still calls this function.

But command pwd and built pwd work as expected

So we can have alias rg="rg --hiddeng", but not rg(){ rg --hidden} (Otherwise we will meet such error : maximum nested function level reached; increase FUNCNEST?)

ref:
man zshall

Answered By: Good Pen
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.