Kumar Alok
Kumar Alok

Reputation: 2612

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.

Upvotes: 41

Views: 24933

Answers (9)

Bruno
Bruno

Reputation: 621

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.

Upvotes: 0

Bob Stack
Bob Stack

Reputation: 113

# 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

Upvotes: 6

mitchnull
mitchnull

Reputation: 6331

If you just want to make sure that the alias doesn't exist, just unalias it and redirect its error to /dev/null like this:

unalias foo 2>/dev/null

You can check if an alias is set with something like this:

alias foo >/dev/null 2>&1 && echo "foo is set as an alias" || echo "foo is not an alias"

As stated in the manpage:

For each name in the argument list for which no  value  is  sup-
plied,  the  name  and  value  of  the  alias is printed.  Alias
returns true unless a name is given for which no alias has  been
defined.

Upvotes: 51

codeforester
codeforester

Reputation: 42999

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 ...

Upvotes: 0

noonex
noonex

Reputation: 2075

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

$ echo ${BASH_ALIASES[ls]}

Upvotes: 7

Tom Hale
Tom Hale

Reputation: 46775

From here:

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

Upvotes: 1

R Sahu
R Sahu

Reputation: 206577

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()
}

Upvotes: 2

kenorb
kenorb

Reputation: 166379

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.

Upvotes: 9

Pablo Fernandez heelhook
Pablo Fernandez heelhook

Reputation: 12503

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.

Upvotes: 16

Related Questions