I recently installed Ubuntu 20.04. I am unable to find the alias command. When I enter which alias
I get nothing back. Same with whereis alias
.
2 Answers
alias
is a shell builtin, so which
and whereis
don't find it. To find out what type of command something is, as well as where its executable is located if it has one (i.e., if it is an external command), you can use type
:
$ type alias
alias is a shell builtin
type
is also a shell builtin:
$ type type
type is a shell builtin
When you run type
on an external command--one that exists as a separate executable file from your shell--type
will give you the details about that. For example, which
is itself an external command:
$ type which
which is /usr/bin/which
Because which
is an external command, it doesn't know about commands that are provided by your shell, which in Bourne-style shells are the shell builtins and shell keywords.
It also doesn't know about commands that you make yourself as shell functions or aliases. For example:
$ f() { printf 'Hello, world.\n'; }
$ which f
$ type f
f is a function
f ()
{
printf 'Hello, world.\n'
}
$ alias g="printf 'Hello, world.\n'"
$ which g
$ type g
g is aliased to `printf 'Hello, world.\n''
Furthermore, it is possible to have an external command that is also provided as some other kind of command, such as a shell builtin. The external command is then said to be "shadowed" by that other command. Running which
will show you the external command, even though that's not what will actually run from your shell. For example, Bash provides printf
as a builtin:
$ which printf
/usr/bin/printf
$ type printf
printf is a shell builtin
Likewise, an external command can be shadowed by a function or alias. You most likely have an ls
alias that expands to an invocation of the external command ls
:
$ which ls
/usr/bin/ls
$ type ls
ls is aliased to `ls --color=auto'
All Bourne-style shells have a type
command, which can be used as shown in any of the above examples. Some Bourne-style shells, including Bash, also allow you to pass the -a
option to type
so that it shows you all the commands of the name you've given. For example:
$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
The meaning of that output is that when you run ls
from your shell, it is running an alias which expands to ls --color=auto
, but if you didn't have that alias (or if you were to remove it), then ls
would run the executable file /usr/bin/ls
, and if that didn't exist, then it would run the executable file /bin/ls
. (On my system, those two executables are actually the same file, accessible through different paths, but the shell doesn't know or care about that.) Your output may vary.
When using Bash or another Bourne-style shell, you should generally use type
instead of which
, as it gives you more reliable information. If, in cases when something is an external command, you prefer to see only the path (i.e., to see output in the style which
provides), you may want to use commmand -v
instead of either type
or which
. In cases where which
is correct, command -v
gives the same output:
$ type file
file is /usr/bin/file
$ which file
/usr/bin/file
$ command -v file
/usr/bin/file
In cases where which
is wrong (relative to the actual behavior of the shell you are using), command -v
still does not mislead you:
$ type ls
ls is aliased to `ls --color=auto'
$ which ls
/usr/bin/ls
$ command -v ls
alias ls='ls --color=auto'
In contrast, even though the whereis
command doesn't know about anything in your shell, there's no single command that should generally be preferred to whereis
. The reason is that whereis
shows you more than just where an external command is located. It tries to also show you where some other important files related to the command are. If this is what you're looking for, you can run both type
and whereis
to avoid being mislead. In most cases, running just type
will tell you what you need to know.
Finally, and regarding the alias
command you asked about: since alias
is a shell builtin, it does not have its own manual page. On most Ubuntu systems, running man alias
has the same effect as running man bash_builtins
, showing a list of Bash builtins but not giving specific information on any of them.
To get help on shell builtins and shell keywords in Bash, you can use the help
command, which is itself a shell builtin. help alias
will show you the help on alias
; help help
will show you the help on help
itself. (See also help type
.)
You may also find the Bash reference manual -- as well as the Bash manpage, which you can view by running man bash
-- to be useful. They have sections documenting the various shell builtins and shell keywords.
It might be an issue with your shell, are you using bash?
You can not find alias with which
or wheris
since it is not a program but a shell function.
-
1
alias
is a shell builtin rather than a shell function, but besides that this is correct, and indeed this is whywhich
andwhereis
don't find it. May 30, 2020 at 4:10
which
command work forcd
? I can't find the executable forcd
either! and Why “which” returns none on some commands I can run?