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

up vote 357 down vote accepted

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;
        echo "$REPLY";
        grep --color=auto "$@"
    }
}
  • what to do when an alias contains MORE aliases? – user251046 Jul 26 '14 at 10:34
  • 1
    @user251046 keep using type until you hit something other than an alias ... – geirha Sep 3 '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 2 at 4:21

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.

  • 10
    Or type alias ls to find out what specifically ls is aliased to. – poolie Feb 7 '12 at 4: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 7 '12 at 4:52
  • 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 6:38

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
  • have this an equivalent on other distros? – sepehr Jul 3 '14 at 13:40
  • @sepehr Works on Debian, I assume it's a bash feature and should work on any distribution. – Der Hochstapler Jul 3 '14 at 16:15
  • 3
    you're right, it works on bash but I have zsh and it doesn't work unfortunately. – sepehr Jul 4 '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
  • How to accomplish this on bash OSX? – Govind Rai Jan 9 '17 at 4:31

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

$ echo ${BASH_ALIASES[ls]}
ls -F --color=auto --show-control-chars
  • 1
    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 6 '17 at 20:16
  • this isn't working in zsh – ProGrammar Apr 18 at 13:22
  • @ProGrammar the question was about bash - for zsh you should look questions about zsh – noonex Apr 18 at 20:19

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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.