'How can I check in my bashrc if an alias was already set

How can I check in my .bashrc if an alias was already set.

When I source a .bashrc file, which has a function name, say fun, and my current environment has an alias as fun also.

I tried unalias fun, but that will give me an error that fun not found when my environment wont have that alias already.

So in my .bashrc, in my fun function I want to check if alias was set, then unalias that.



Solution 1:[1]

Just use the command alias like

alias | grep my_previous_alias

Note that you can actually use unalias, so you could do something like

[ `alias | grep my_previous_alias | wc -l` != 0 ] && unalias my_previous_alias

That will remove the alias if it was set.

Solution 2:[2]

You can use type to see if the command exists, or whether is alias or not.

It'll return error status, if the command is not found.

For example, I'm defining the following alias:

$ alias foo="printf"

Then check the following scenarios:

$ type foo >/dev/null && echo Command found. || echo Command not found.
Command found.

or specifically for alias:

$ alias foo && echo Alias exists || echo Alias does not exist.

or to check whether it's alias or regular command:

$ grep alias <(type foo) && echo It is alias. || echo It is not.

To check if the alias is defined in your rc files, it needs to be checked manually e.g. by:

[ "$(grep '^alias foo=' ~/.bash* ~/.profile /etc/bash* /etc/profile)" ] && echo Exists. || echo Not there.

Solution 3:[3]

Good bash-specific solution to check aliases is using BASH_ALIASES array, e.g.:

$ echo ${BASH_ALIASES[ls]}

Solution 4:[4]

# Test if alias of name exists, and then remove it:
[ "$(type -t name)" = "alias" ] && unalias name

# Test if function of name exists, and then remove it:
[ "$(type -t name)" = "function" ] && unset -f name

Solution 5:[5]

You can use the following to make your bashrc file simpler:

  1. Make sure that an alias exists.
  2. Unalias it.
  3. Define the function

alias fun=''
unalias fun
fun ()
{
   # Define the body of fun()
}

Solution 6:[6]

From here:

if alias <your_alias_name> 2>/dev/null; then 
  do_something
else 
  do_another_thing; 
fi

Solution 7:[7]

Wrote this function based on noonex's answer:

unalias_if() {
    local alias_name
    for alias_name; do
        [[ ${BASH_ALIASES[$alias_name]} ]] && unalias "$alias_name"
    done
}

and it can be invoked safely as

unalias_if alias1 alias2 ...

Solution 8:[8]

I use something similar to @noonex's proposal, but can handle empty aliases with :

[[ -v BASH_ALIASES[foo] ]] && unalias foo

This will work even if foo alias exists but is empty :

alias foo=
[[ ${BASH_ALIASES[foo]} ]] && echo unalias1
[[ -v BASH_ALIASES[foo] ]] && echo unalias2

Output :

unalias2

Would you need a function like @codeforester's solution, you should change ${BASH_ALIASES[$alias_name]} accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pablo Fernandez heelhook
Solution 2 kenorb
Solution 3 codeforester
Solution 4 Bob Stack
Solution 5 R Sahu
Solution 6 Tom Hale
Solution 7 codeforester
Solution 8 Bruno