217

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 3
    Just a note about some of the answers below: "command" is the ONLY safe way to accomplish this. For example, the "." or "source" built-in maybe be overridden by a script-defined function. "command ." or "command source" is the only way to safely execute source without the script-defined function being invoked instead. None of the other alternatives, including the backslash method ".", work, and of course there is no "/bin/source" alternative that you can invoke by specifying a full path. – Dejay Clayton Jun 17 '15 at 18:46

8 Answers8

286

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.

Not Now
  • 2,572
102

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.

Mikel
  • 57,299
  • 15
  • 134
  • 153
  • 15
    Because it adds 8 times the number of characters as the backslash method – JoelFan May 25 '12 at 13:12
  • 1
    @STATUS_ACCESS_DENIED This also doesn't work with tcsh (and therefore presumably not with csh either). The \\ method does – Levon May 25 '12 at 13:15
  • 1
    @Levon: the question was asked for bash, though ;) ... see the tags. And the reason the other one works is a quirk, nothing else. – 0xC0000022L May 25 '12 at 13:21
  • @STATUS_ACCESS_DENIED ah .. good point re the tag, didn't notice it. Glad the \ works (quirk or not) .. that'll come in useful. – Levon May 25 '12 at 13:23
  • @STATUS_ACCESS_DENIED Thanks for defending my answer. But bash has many quirks, so provided the backslash method is a feature and not a bug, I think it's also a good answer. In fact it's documented in the bash aliases reference. – Mikel May 25 '12 at 16:27
  • 3
    The backslash feature is not secure. While it provides protection against similarly-named aliases, it does not provide protection against similarly-named functions. – Dejay Clayton Jun 17 '15 at 18:48
  • 3
    @JoelFan I'd much rather have the few extra characters and have my scripts be easy to follow and work for everyone. Rather than a cryptic slash which is not intuitive, easy to miss, not well documented, and doesn't work for bypassing functions. – Dennis Dec 18 '18 at 09:52
  • note: command works great when its there (such as on fedora) but i was seeing that some distros do not appear to have it. i didn't see it after running sudo find -type f -iname 'command' under the bin or usr dirs on my linux mint partition (searching from fedora). if writing x-distro scripts, this may not be the best option – zpangwin Apr 20 '21 at 19:57
  • @zpangwin It's not a regular binary so find won't find it. bash manual POSIX – Mikel Apr 21 '21 at 05:19
  • @ Mikel, you are absolutely correct, my bad. I had thought it was just a binary bc fedora-33 has a binary at /usr/bin/command. After looking at docs and running type command from within each distro separately, it is indeed a builtin on both systems despite fedora also having a binary. I also discovered another method which is not portable. On Fedora, it has GNU which v2.21 and that supports the options --skip-alias and --skip-functions. Unfortunately, LM 20 (and presumably Ubuntu 20/Debian 10) are using a different version of which that does NOT support these (or even --version) – zpangwin Apr 21 '21 at 16:46
20

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.

George M
  • 13,959
  • 2
    You have to know where the command is. On some systems, the division between /bin and /usr/bin is arbitrary. – Keith Thompson May 25 '12 at 20:50
  • @KeithThompson For that, you could use which for some commands: which ls. – cst1992 Apr 19 '16 at 11:44
  • @cst1992: If you're going to use which (or type), it does a lookup via $PATH; you might as well just use ls or \ls rather than specifying the path. – Keith Thompson Apr 19 '16 at 15:10
  • @cst1992 I know the OP is about Bash, but on zsh, both which and command -v or its human-readable command -V return the alias versions. Using \ls just works, but to get its real path I can only seem to think of echo "$PATH" | tr ':' '\n' | xargs -I{} find {} -maxdepth 1 -name 'ls' -type f 2>/dev/null | head -n1... which works. – mazunki Feb 27 '22 at 01:36
10

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.

krlmlr
  • 957
4

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.

  • I think this is a good strategy, because what if 'ls' is aliased and then some other random script needs/expects the original behavior, but accidentally uses the "shadowed" ls? – PJ Brunet Mar 18 '23 at 18:39
4

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.

wisbucky
  • 3,388
1

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

asmeurer
  • 331
  • 5
    In which shell, which operating system, which terminal, etc.? – Mikel May 24 '12 at 23:32
  • 1
    @Mikel There is a package "sl" that provides "sl" as "ls". The tips bash gives in Ubuntu also say that "LS" is provided by "sl". – Izkata May 25 '12 at 00:18
  • 1
    This is bash 4 (installed with Fink) in Mac OS X 10.7. It also works for other commands that I have aliased like rm and grep. – asmeurer May 25 '12 at 02:34
  • 3
    It's just a joke :) from the mn page: LS - display animations aimed to correct users who accidentally enter LS instead of ls. – amyassin May 25 '12 at 09:29
  • 8
    I think this works for Mac OS X because its filesystem is case-insensitive by default. It won't work on other unixes. – Jander May 25 '12 at 13:30
  • @Jander that's likely it. Still, it's quite useful to take advantage of if you're on such a system. – asmeurer May 25 '12 at 19:24
0

For zsh

Order:

  1. you type in the shell (stdin)
  2. history expansion, such as !!, !-3
  3. alias expansion (try ctrl-x 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

Good Pen
  • 205