I have a bash script that needs to behave differently if a particular alias is defined. Is there a way to test if a particular command is an alias in bash?

  • 11
    type -t <command>
    – jimmij
    Jun 8, 2016 at 18:55
  • 1
    @jimmij: You should make that into an answer.
    – Dan
    Jun 8, 2016 at 21:31

2 Answers 2

if alias <your_alias_name> 2>/dev/null; then 
  • 2
    works great! you will need to hide STDOUT as well if the alias exists.
    – marinara
    Jun 22, 2020 at 7:58
  • 1
    If anyone is looking to do the same as @marinara is advising then you can do &> /dev/null or 2>&1 > /dev/null instead.
    – shriek
    Jul 23, 2021 at 0:12
  • @shriek . Thanks for giving the example. I'm pretty sure that the &> /dev/null is great (as long as you are in bash and your version is new enough). However, I'm pretty sure that you need to switch - instead of 2>&1 >/dev/null #wrong, do >/dev/null 2>&1 #right. In the former, stderr is told to go where stdout is going, then stdout is moved to a different place ( /dev/null ). I'm pretty sure that stderr won't follow stdout to the new place. I'm going to do some experiments to check. Feb 10 at 2:38
  • I checked. TL;DR You should use &>/dev/null or >/dev/null 2>&1. (I usually have to check with an experiment.) Here's how I'll show my results. I'll use an unaliased a, which will then be aliased to ls. I'll use a decently well-known directory with few files - /etc/dbus-1. My format will be $ <command1> #comments1 ; output1 ||| $ <command2> #comments2 ; output2 ||| command3 (etc.). with '' representing no output. Let's go! Feb 10 at 3:11
  • $ pwd ; /etc/dbus-1|||$ alias a ; bash: alias: a: not found|||$ alias a >/dev/null #goes to stdout? no-will see error ; bash: alias: a: not found|||$ alias a 2>/dev/null #goes to stderr? yes-won't see err ; ''|||$ alias a 2>&1 >/dev/null #incorrect order=>symptom-will see err ; bash: alias: a: not found|||$ alias a >/dev/null 2>&1 #correct order=>no symptom-won't see err ; ''|||$ alias a 1>/dev/null 2>/dev/null #alternate correct-won't see err ; ''|||$ alias a &>/dev/null #also correct-won't see err ''|||$ ls ; session.conf session.d system.conf system.d Feb 10 at 5:06

I’m gonna make @jimmij’s comment from 5 years ago into an answer:

type -t is designed for exactly that purpose. It will output alias if the given command is an alias. If the command does not exist, it won’t output anything, which means you don’t need to redirect its stderr or something.


if [ "$(type -t foo)" = 'alias' ]; then
    echo 'foo is an alias'
    echo 'foo is not an alias'

Note that the -t switch is not defined in POSIX. Shells other than bash might not implement it.

  • 1
    That's a very good solution. Thanks for giving the might-only-work-in-bash note. Another thanks for putting that long-ago comment into an answer. That's how to be a good citizen. Feb 10 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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