bash 如果已设置别名,如何检查我的 bashrc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9783507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:48:59  来源:igfitidea点击:

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

aliasingbash

提问by Kumar Alok

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

.bashrc如果已设置别名,我如何检查我的信息。

When I source a .bashrcfile, which has a function name, say fun, and my current environment has an alias as funalso.

当我获取一个.bashrc具有函数名称的文件时,请说fun,并且我当前的环境也有一个别名fun

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

我尝试了unaliasfun,但这会给我一个错误,当我的环境没有该别名时,找不到 fun。

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

所以在我的.bashrc, 在我的 fun 函数中,我想检查是否设置了别名,然后unalias是。

回答by mitchnull

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:

如果您只想确保别名不存在,只需取消别名并将其错误重定向到 /dev/null ,如下所示:

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.

回答by Pablo Fernandez heelhook

Just use the command aliaslike

只需使用alias像这样的命令

alias | grep my_previous_alias

Note that you canactually use unalias, so you could do something like

请注意,您实际上可以使用unalias,因此您可以执行以下操作

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

That will remove the alias if it was set.

如果设置了别名,这将删除别名。

回答by kenorb

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

您可以使用type查看命令是否存在,或者是否是别名。

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 rcfiles, it needs to be checked manually e.g. by:

要检查别名是否在rc文件中定义,需要手动检查,例如:

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

回答by noonex

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

检查别名的特定于 bash 的良好解决方案是使用BASH_ALIASES数组,例如:

$ echo ${BASH_ALIASES[ls]}

回答by R Sahu

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

您可以使用以下内容使您的 bashrc 文件更简单:

  1. Make sure that an alias exists.
  2. Unalias it.
  3. Define the function
  1. 确保存在别名。
  2. 取消别名。
  3. 定义函数


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

回答by Tom Hale

From here:

这里

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

回答by codeforester

Wrote this function based on noonex'sanswer:

根据noonex 的回答写了这个函数:

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