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?
4 Answers
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
do_something
else
do_another_thing;
fi
(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.
Example:
if [ "$(type -t foo)" = 'alias' ]; then
echo 'foo is an alias'
else
echo 'foo is not an alias'
fi
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"
else
echo "Cannot find $1"
fi
}
# == MAIN ==
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <command> [ <command> ...]"
exit 1
fi
for cmd in $@; do
is_alias $cmd
done
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"
Example:
[[ $(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 tols -l
, but[[ $(type ll) == *"alias" ]] && echo "alias defined" || echo "alias undefined"
resultsalias 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. Mar 25 at 13:51 -
This does not work in
bash
as thetype
command never outputs anything that starts with the stringalias
.– Kusalananda ♦Mar 25 at 14:49 -
1Your modified answer would say that
ll
was an alias if the stringll=
occurs in any alias definition, regardless of whetherll
was an alias or not. (A solution forzsh
would look identical to a solution forbash
, BTW).– Kusalananda ♦Mar 26 at 19:28
type -t <command>