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?
-
3Just 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– Dejay Clayton2015-06-17 18:46:20 +00:00Commented Jun 17, 2015 at 18:46
9 Answers
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.
-
very nice, also works well with tcshLevon– Levon2012-05-25 13:15:47 +00:00Commented May 25, 2012 at 13:15
-
14This way is very useful. It only bypasses aliases, not functions, which makes it different from
commandorbuiltin.Mikel– Mikel2012-05-25 16:31:51 +00:00Commented May 25, 2012 at 16:31 -
1It seems like
l\sdoes the same thing. Is this behaviour a bug or a feature?Niklas B.– Niklas B.2012-05-25 16:46:05 +00:00Commented May 25, 2012 at 16:46 -
6It's a "feature". Only unquoted aliases are expanded. So you can put it in quotes, or use a backslash anywhere you like. See bash aliases reference.Mikel– Mikel2012-05-25 17:55:05 +00:00Commented May 25, 2012 at 17:55
-
2In case these solutions aren't working, do
type -a lsto troubleshoot. unix.stackexchange.com/questions/39291/…wisbucky– wisbucky2021-08-06 19:09:29 +00:00Commented Aug 6, 2021 at 19:09
That's what the command command is for.
Try
command ls
This tells the shell to bypass aliases and functions.
-
15Because it adds 8 times the number of characters as the backslash methodJoelFan– JoelFan2012-05-25 13:12:03 +00:00Commented May 25, 2012 at 13:12
-
1@STATUS_ACCESS_DENIED This also doesn't work with tcsh (and therefore presumably not with csh either). The `\` method doesLevon– Levon2012-05-25 13:15:11 +00:00Commented May 25, 2012 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– 0xC0000022L2012-05-25 13:21:12 +00:00Commented May 25, 2012 at 13:21 -
4The backslash feature is not secure. While it provides protection against similarly-named aliases, it does not provide protection against similarly-named functions.Dejay Clayton– Dejay Clayton2015-06-17 18:48:38 +00:00Commented Jun 17, 2015 at 18:48
-
5@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– Dennis2018-12-18 09:52:19 +00:00Commented Dec 18, 2018 at 9:52
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.
-
2You have to know where the command is. On some systems, the division between
/binand/usr/binis arbitrary.Keith Thompson– Keith Thompson2012-05-25 20:50:13 +00:00Commented May 25, 2012 at 20:50 -
@KeithThompson For that, you could use
whichfor some commands:which ls.cst1992– cst19922016-04-19 11:44:10 +00:00Commented Apr 19, 2016 at 11:44 -
@cst1992: If you're going to use
which(ortype), it does a lookup via$PATH; you might as well just uselsor\lsrather than specifying the path.Keith Thompson– Keith Thompson2016-04-19 15:10:43 +00:00Commented Apr 19, 2016 at 15:10 -
@cst1992 I know the OP is about Bash, but on
zsh, bothwhichandcommand -vor its human-readablecommand -Vreturn the alias versions. Using\lsjust works, but to get its real path I can only seem to think ofecho "$PATH" | tr ':' '\n' | xargs -I{} find {} -maxdepth 1 -name 'ls' -type f 2>/dev/null | head -n1... which works.mazunki– mazunki2022-02-27 01:36:29 +00:00Commented Feb 27, 2022 at 1:36
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.
-
What's the brackets for? I guess you mean
`which ls`to execute the binary directly.amyassin– amyassin2012-05-25 09:34:38 +00:00Commented May 25, 2012 at 9:34 -
2@amyassin: Both syntaxes are equivalent. (BTW, how did you get the backticks into the code formatting?)krlmlr– krlmlr2012-05-25 11:25:39 +00:00Commented May 25, 2012 at 11:25
-
Actually the
(which ls)didn't work for me (on Ubuntu 12.04 LTS) And the backticks were hard, I just escaped them: \`which ls\`. It took a lot of editing to get there :)amyassin– amyassin2012-05-25 15:39:55 +00:00Commented May 25, 2012 at 15:39 -
1@user946850 "To include a literal backtick character within a code span, you can use multiple backticks as the opening and closing delimiters". Source: Markdown Reference.Mikel– Mikel2012-05-25 16:24:01 +00:00Commented May 25, 2012 at 16:24
-
@amyassin: Did you add the
$?krlmlr– krlmlr2012-05-25 16:34:43 +00:00Commented May 25, 2012 at 16:34
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?Jay Brunet– Jay Brunet2023-03-18 18:39:43 +00:00Commented Mar 18, 2023 at 18:39
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.
Typing the command in uppercase seems to work, like LS, though I'm not really sure why.
-
5In which shell, which operating system, which terminal, etc.?Mikel– Mikel2012-05-24 23:32:22 +00:00Commented May 24, 2012 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– Izkata2012-05-25 00:18:52 +00:00Commented May 25, 2012 at 0:18
-
1This 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– asmeurer2012-05-25 02:34:05 +00:00Commented May 25, 2012 at 2:34
-
3It's just a joke :) from the mn page:
LS - display animations aimed to correct users who accidentally enter LS instead of ls.amyassin– amyassin2012-05-25 09:29:44 +00:00Commented May 25, 2012 at 9:29 -
8I think this works for Mac OS X because its filesystem is case-insensitive by default. It won't work on other unixes.Jander– Jander2012-05-25 13:30:44 +00:00Commented May 25, 2012 at 13:30
One way that should be portable and independent of a shell type is to use env:
env- run a program in a modified environmentSYNOPSIS
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
So running env ls [args] always invokes /bin/ls (or whichever is in PATH).
-
🖒 . Note however that busybox
envwill likely (may be dependant on build time configuration settings) run the busyboxlsapplet rather than the one found in$PATH. Same applies to thecommandbuiltin of its shellStéphane Chazelas– Stéphane Chazelas2024-05-15 07:00:35 +00:00Commented May 15, 2024 at 7:00
For zsh
Order:
- you type in the shell (stdin)
- history expansion, such as
!!,!-3 - alias expansion (try ctrl-x then a, to see the expanded result)
- 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',\pwdor"pwd"(`pwd` is not regarded as quoting) builtin pwduse zsh' s built-in function(aka command?)pwdcommand pwduse /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