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?

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

4 Answers 4


If alias is passed an alias name without =value, it just prints the alias definition if that alias is defined, or fails with an error if there's no such alias.

So you can just do:

if alias your_alias_name >/dev/null 2>&1; then 

(replace your_alias_name as required)


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.


Word of warning:

Shell functions and aliases are limited to the shell and do not work in executed shell scripts.

Found in https://askubuntu.com/a/98796 , confirmed by my own bitter experience.

Somewhat annoyingly this is not mentioned explicitly in the relevant section of the Bash user manual, the only hint is that

Alias expansion (see Aliases) is performed by default.

which was not intuitive enough for me; YMMV.

In short, all the technologies mentioned in the other answers work only if you check aliases defined in the same script (or in another file you source-d into your script). You cannot check from within a (non-interactive) script whether an alias was defined in the shell where you invoked said script.

Here is a little script aliastest.sh that demonstrates the issue:

#!/usr/bin/env bash

shopt -s expand_aliases

is_alias() {
    if [[ "$(type -t $1)" == "alias" ]]; then
        echo "$1 is an alias"
        echo "Cannot find $1"

# == MAIN ==

if [[ $# -lt 1 ]]; then
    echo "Usage: $0 <command> [ <command> ...]"
    exit 1

for cmd in $@; do
    is_alias $cmd

Let's assume you defined the popular ll alias in your shell with the command alias ll='ls -alF' (you may have it already). If you run ./aliastest.sh ll it will tell you Cannot find ll.

But despair not, there is a workaround:

If you tell Bash to run your script as "interactive", then it'll "see" the aliases defined in the shell you invoked it from:

bash -i aliastest.sh ll

It will answer ll is an alias.


You can write like this

[[ $(type your_alias) == *"alias" ]] && echo "alias defined" || echo "alias undefined"


[[ $(type fd) == *"alias" ]] && echo "fd is an alias" || echo "fd is not an alias"

Another option can be

[[ -z $(alias | grep 'll=') ]] && echo "alias not found" || echo "alias found"
  • Deb 11, not working as presented. ll is alias to ls -l, but [[ $(type ll) == *"alias" ]] && echo "alias defined" || echo "alias undefined" results alias undefined. If is isn't the way your suggested answer should be understood, you should edit it so that how this is supposed to be done is clear. Commented Mar 25, 2023 at 13:51
  • This does not work in bash as the type command never outputs anything that starts with the string alias.
    – Kusalananda
    Commented Mar 25, 2023 at 14:49
  • 1
    Your modified answer would say that ll was an alias if the string ll= occurs in any alias definition, regardless of whether ll was an alias or not. (A solution for zsh would look identical to a solution for bash, BTW).
    – Kusalananda
    Commented Mar 26, 2023 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .