我该如何在我的.bashrc
中检查别名是否已经设置。
当我引用一个.bashrc
文件时,其中有一个函数名称,比如说fun,而我的当前环境也有一个别名作为fun。
我尝试使用unalias
fun,但当我的环境没有该别名时,这将导致错误fun not found。
因此,在我的.bashrc
中,我想要在我的fun函数中检查是否设置了别名,然后unalias
它。
我该如何在我的.bashrc
中检查别名是否已经设置。
当我引用一个.bashrc
文件时,其中有一个函数名称,比如说fun,而我的当前环境也有一个别名作为fun。
我尝试使用unalias
fun,但当我的环境没有该别名时,这将导致错误fun not found。
因此,在我的.bashrc
中,我想要在我的fun函数中检查是否设置了别名,然后unalias
它。
如果您只想确保别名不存在,只需取消别名并将其错误重定向到 /dev/null,就像这样:
unalias foo 2>/dev/null
您可以通过以下方式检查别名是否设置:alias foo >/dev/null 2>&1 && echo "foo is set as an alias" || echo "foo is not an alias"
如man手册所述:
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.
只需使用命令alias
,例如:
alias | grep my_previous_alias
请注意您 实际上可以 使用 unalias
命令,这样您就可以执行类似以下的操作:
[ `alias | grep my_previous_alias | wc -l` != 0 ] && unalias my_previous_alias
如果已经设置了别名,那么这将删除该别名。
la
。在我的Mac的.bash_profile
文件中,我有alias la='ls -GA'
(因为G代表彩色),然后在我的.bashrc
文件中,我有function checkLa(){ if [ "$(alias | grep la)" == "ls -GA" ]; then echo "mac user!"; return 0; fi echo "linux user!"; alias la='ls -A --color=auto' }; checkLa()
。我知道alias会返回所有的别名,但我只是没有想到它!:D - dylnmc您可以使用type
命令来查看命令是否存在,或者它是否是别名。
如果找不到该命令,则会返回错误状态。
例如,我正在定义以下别名:
$ alias foo="printf"
接下来请检查以下情况:
$ type foo >/dev/null && echo Command found. || echo Command not found.
Command found.
或者特定于别名:
$ alias foo && echo Alias exists || echo Alias does not exist.
或者检查它是别名还是普通命令:
$ grep alias <(type foo) && echo It is alias. || echo It is not.
要检查别名是否在您的rc文件中定义,需要手动检查,例如:
[ "$(grep '^alias foo=' ~/.bash* ~/.profile /etc/bash* /etc/profile)" ] && echo Exists. || echo Not there.
检查别名的好方法是使用 BASH_ALIASES
数组,例如:
$ echo ${BASH_ALIASES[ls]}
[[ ${BASH_ALIASES[foo]} ]] && unalias foo
这样做就可以了,对吧? - codeforester[[ ${BASH_ALIASES[foo]} ]] && unalias foo
是不正确的(别名可能存在但为空:alias foo=
)。我改用了 [[ -v BASH_ALIASES[foo] ]] && unalias foo
。 - Bruno# 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
alias fun=''
unalias fun
fun ()
{
# Define the body of fun()
}
我使用类似于@noonex提出的方案,但可以处理空别名:
[[ -v BASH_ALIASES[foo] ]] && unalias foo
foo
别名存在但为空:alias foo=
[[ ${BASH_ALIASES[foo]} ]] && echo unalias1
[[ -v BASH_ALIASES[foo] ]] && echo unalias2
输出:
unalias2
如果您需要像@codeforester
的解决方案一样的函数,则应相应更改${BASH_ALIASES[$alias_name]}
。
根据noonex的答案编写了这个函数:
unalias_if() {
local alias_name
for alias_name; do
[[ ${BASH_ALIASES[$alias_name]} ]] && unalias "$alias_name"
done
}
并且可以安全地调用
unalias_if alias1 alias2 ...
[[ -v BASH_ALIASES[$alias_name] ]]
。 - Bruno
unaalias foo 2>/dev/null || echo "The foo alias existed and has been undefined"
即使不存在也会始终打印它存在,并且如果存在也不会取消定义;您永远不会看到告诉您实际情况的bash:unaalias:command not found
错误消息。我建议使用Bob Stack的答案,它没有这个问题;它确保您将看到与编程错误相关的错误消息。 - cjs-q
到unalias
命令 :)。补丁链接:https://github.com/bminor/bash/compare/master...mitchnull:bash:add-quiet-mode-to-unalias。 - mitchnull