Suppose I have an alias in the bash shell. Is there a simple command to print out what command the alias will run?
6 Answers
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.
- 2what to do when an alias contains MORE aliases?user251046– user2510462014-07-26 10:34:04 +00:00Commented Jul 26, 2014 at 10:34
- 4@user251046 keep using
typeuntil you hit something other than an alias ...geirha– geirha2014-09-03 19:03:28 +00:00Commented Sep 3, 2014 at 19:03 - 1I like this answer because
typewill parse/interpret any quotes, so you can make sure the quotes are right.wisbucky– wisbucky2018-03-02 04:21:39 +00:00Commented Mar 2, 2018 at 4:21 - 1I 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– krubo2019-01-29 02:19:45 +00:00Commented Jan 29, 2019 at 2:19 - 6@krubo
type -a lswill 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– geirha2019-01-30 07:54:47 +00:00Commented Jan 30, 2019 at 7:54
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.
- 21Or type
alias lsto find out what specificallylsis aliased to.poolie– poolie2012-02-07 04:10:34 +00:00Commented Feb 7, 2012 at 4:10 - 3@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.2012-02-07 04:52:10 +00:00Commented Feb 7, 2012 at 4:52
- 4while this works for aliases, it doesn't work if you've defined a custom shell function.
typehowever, works in both cases.Sujay Phadke– Sujay Phadke2016-09-24 06:38:32 +00:00Commented Sep 24, 2016 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 - 2have this an equivalent on other distros?sepehr– sepehr2014-07-03 13:40:56 +00:00Commented Jul 3, 2014 at 13:40
- 2@sepehr Works on Debian, I assume it's a bash feature and should work on any distribution.Oliver Salzburg– Oliver Salzburg2014-07-03 16:15:50 +00:00Commented Jul 3, 2014 at 16:15
- 6you're right, it works on bash but I have zsh and it doesn't work unfortunately.sepehr– sepehr2014-07-04 12:46:21 +00:00Commented Jul 4, 2014 at 12:46
- 3How to accomplish this on bash OSX?Govind Rai– Govind Rai2017-01-09 04:31:09 +00:00Commented Jan 9, 2017 at 4:31
- 2It 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 %Rinstead ofsqueue -u davidmh -o "%.18i %.9P %.25j %.8u %.8T %.10M %.9l %.6D %R "Davidmh– Davidmh2018-05-04 19:19:25 +00:00Commented May 4, 2018 at 19:19
Strictly speaking correct answer is using BASH_ALIASES array, e.g.:
$ echo ${BASH_ALIASES[ls]} ls -F --color=auto --show-control-chars - 5I 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– M. Justin2017-03-06 20:16:07 +00:00Commented Mar 6, 2017 at 20:16
- 1this isn't working in zshProGrammar– ProGrammar2018-04-18 13:22:14 +00:00Commented Apr 18, 2018 at 13:22
- 2@ProGrammar the question was about bash - for zsh you should look questions about zshnoonex– noonex2018-04-18 20:19:48 +00:00Commented Apr 18, 2018 at 20:19
- 3Bingo. Exactly what I needed, same as @M.Justin - I want to stack more switches onto the current
lsalias without altering what's there. So I'm goingalias ls="${BASH_ALIASES[ls]} --time-style=iso"for my case.Rich– Rich2019-05-29 19:17:00 +00:00Commented May 29, 2019 at 19:17 - 2Great for 'watch' command which typically doesn't see aliases: watch $(echo "${BASH_ALIASES[ll]}")Rondo– Rondo2022-02-18 03:54:26 +00:00Commented Feb 18, 2022 at 3:54
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.
- 1bash has no
whichcommand.geirha– geirha2014-09-11 09:34:10 +00:00Commented Sep 11, 2014 at 9:34 - 3
- 5
whichis a bad way to lookup aliases as explained here: unix.stackexchange.com/questions/10525/… It doesn't even work for me for aliases in bash on Ubuntu.Sujay Phadke– Sujay Phadke2016-09-24 06:36:40 +00:00Commented Sep 24, 2016 at 6:36 - 1Only solution that worked for me on MacOS/zsh (This is a Bash question on AskUbuntu I know, but AskUbuntu shows up first in Google)Chris Hayes– Chris Hayes2021-11-15 16:48:50 +00:00Commented Nov 15, 2021 at 16:48
At terminal
$ alias | grep ALIAS Outputs
ALIAS='some_command' Replace ALIAS with your alias.
- 4Redundant. Just run
alias <command>Fadi– Fadi2022-02-21 00:42:00 +00:00Commented Feb 21, 2022 at 0:42