553

Suppose I have an alias in the bash shell. Is there a simple command to print out what command the alias will run?

RolandiXor
  • 51,541
Casebash
  • 5,789

6 Answers6

725

The type builtin is useful for this. It will not only tell you about aliases, but also functions, builtins, keywords and external commands.

$ type ls
ls is aliased to `ls --color=auto'
$ type rm
rm is /bin/rm
$ type cd
cd is a shell builtin
$ type psgrep
psgrep is a function
psgrep () 
{ 
    ps -ef | { 
        read -r;
        printf '%s\n' "$REPLY";
        grep --color=auto "$@"
    }
}

type -a cmd will show all the commands by that name in order of precedence, which is useful for the ls alias above, where the alias itself calls ls.

$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls

This tells you that when you run ls, /bin/ls will be used, and --color=auto will be included in its list of arguments, in addition to any other you add yourself.

geirha
  • 46,101
  • 1
    what to do when an alias contains MORE aliases? – user251046 Jul 26 '14 at 10:34
  • 3
    @user251046 keep using type until you hit something other than an alias ... – geirha Sep 03 '14 at 19:03
  • I like this answer because type will parse/interpret any quotes, so you can make sure the quotes are right. – wisbucky Mar 02 '18 at 04:21
  • I got ls is aliased to 'ls --color=auto', but how can I get one layer deeper, to see whether it uses /bin/ls or /usr/local/bin/ls or what? – krubo Jan 29 '19 at 02:19
  • 4
    @krubo type -a ls will show all ls commands found in order of preference. Whichever is right below the alias is the one that will be executed by the alias. – geirha Jan 30 '19 at 07:54
  • type -a ls is exactly what I was looking for, thanks! – krubo Jan 30 '19 at 18:24
  • cool, I've been using which for years, but that fails on aliases and functions (at least on my system, YMMV). Have to remember to use type instead... – rob74 Oct 15 '20 at 07:08
245

Just type alias while at the Shell prompt. It should output a list of all currently-active aliases.

Or, you can type alias [command] to see what a specific alias is aliased to, as an example, if you wanted to find out what the ls alias was aliased to, you could do alias ls.

Thomas Ward
  • 74,764
  • 18
    Or type alias ls to find out what specifically ls is aliased to. – poolie Feb 07 '12 at 04:10
  • 2
    @poolie Indeed. I think the question was to see all the aliases, though, which is why i did not elaborate further on the alias command. – Thomas Ward Feb 07 '12 at 04:52
  • 3
    while this works for aliases, it doesn't work if you've defined a custom shell function. type however, works in both cases. – Sujay Phadke Sep 24 '16 at 06:38
76

I really like Ctrl+Alt+E as I learned from this answer. It "expands" the currently typed command line, meaning it performs alias expansion (amongst other things).

What does that mean? It turns any alias, that might be currently written on the command line, into what the alias stands for.

For example, if I type:

$ ls

and then press Ctrl+Alt+E, it is turned into

$ ls --time-style=locale --color=auto
  • 1
    have this an equivalent on other distros? – sepehr Jul 03 '14 at 13:40
  • 1
    @sepehr Works on Debian, I assume it's a bash feature and should work on any distribution. – Oliver Salzburg Jul 03 '14 at 16:15
  • 5
    you're right, it works on bash but I have zsh and it doesn't work unfortunately. – sepehr Jul 04 '14 at 12:46
  • was really helpful. I had a different goal of expanding one of the previous bash commands logged in history with, i.e., !394, so that I could edit it first before executing – XXL Mar 23 '16 at 12:42
  • 1
    How to accomplish this on bash OSX? – Govind Rai Jan 09 '17 at 04:31
  • 1
    It has one caveat. When an alias includes necessary quotes, they will be removed. So, I get squeue -u davidmh -o %.18i %.9P %.25j %.8u %.8T %.10M %.9l %.6D %R instead of squeue -u davidmh -o "%.18i %.9P %.25j %.8u %.8T %.10M %.9l %.6D %R " – Davidmh May 04 '18 at 19:19
  • any Mac equivalent? – nonopolarity Apr 05 '20 at 16:25
  • Dove into this rabbit-hole today. This feature is brought to you by readline. You can see readline's current key-mappings by typing bind -P. You can modify readline's keymappings by creating/editing its config file ~/.inputrc. Get more info with man readline. – JS. Mar 09 '23 at 00:26
24

Strictly speaking correct answer is using BASH_ALIASES array, e.g.:

$ echo ${BASH_ALIASES[ls]}
ls -F --color=auto --show-control-chars
noonex
  • 357
  • 3
    I found this useful in a situation where I wanted programmatic access to the actual statement being aliased without the human-useful stuff around it. – M. Justin Mar 06 '17 at 20:16
  • this isn't working in zsh – ProGrammar Apr 18 '18 at 13:22
  • 1
    @ProGrammar the question was about bash - for zsh you should look questions about zsh – noonex Apr 18 '18 at 20:19
  • 1
    Bingo. Exactly what I needed, same as @M.Justin - I want to stack more switches onto the current ls alias without altering what's there. So I'm going alias ls="${BASH_ALIASES[ls]} --time-style=iso" for my case. – Rich May 29 '19 at 19:17
  • 1
    Great for 'watch' command which typically doesn't see aliases: watch $(echo "${BASH_ALIASES[ll]}") – Rondo Feb 18 '22 at 03:54
6

You could use the which command.

If you set an alias for ls as ls -al and then type which ls, you will see:

ls: aliased to ls -al.

Luís de Sousa
  • 13,227
  • 26
  • 81
  • 128
3

At terminal

$ alias | grep ALIAS

Outputs

ALIAS='some_command'

Replace ALIAS with your alias.

TrySpace
  • 139