我在.bashrc
中添加了一些别名,作为git命令的快捷方式。
一个例子是ga
for git add
但是,当我对ga
函数做了一些更改时,比如:
function ga() {
echo "hello"
}
并且在终端中使用ga
,它还在使用git add
。
我试图通过编辑ga
,然后使用source ~/.bashrc
来注释掉.bashrc。但是,它仍然执行别名而不是函数。
原因是什么?
发布于 2021-01-21 02:20:22
在定义别名时,必须考虑到在函数之前查找它们:
$ alias hello="echo 'this is a hello alias'"
$ function hello() { echo "this is a hello function"; }
$ hello
this is a hello alias
# ^^^^^ <--- it executes the alias, not the function!
那么调用函数的方法是什么呢?只需在名称之前使用。它将绕过别名:
$ \hello
this is a hello function
# ^^^^^^^^ <--- it executes the function now
您还可以使用unalias
,因此可以删除别名。
$ unalias hello
$ hello
this is a hello function
# ^^^^^^^^
如果别名和函数有命令的名称怎么办?然后使用command
就方便了
$ alias date="echo 'this is an alias on date'"
$ function date() { echo "this is a function on date"; }
$ date
this is an alias on date
# ^^^^^ <--- it executes the alias, not the function!
$ \date
this is a function on date
# ^^^^^^^^ <--- it executes the function
$ command date
Thu Jan 21 10:56:20 CET 2021
# ^^^^^^^^^^^^^^^^^^^^^^^^^ <--- it executes the command
您也可以使用nice
$ nice -n0 date
Thu Jan 21 10:56:20 CET 2021
如别名与函数与脚本所示
在函数之前查找别名:如果同时有一个函数和一个名为
foo
的别名,则foo
调用别名。(如果要扩展别名foo
,则会暂时阻塞它,这会使类似于alias ls='ls --color'
的事情正常工作。而且,通过运行\foo
,您可以在任何时候绕过别名。)不过,我不希望看到一个可衡量的性能差异。
进一步读:
发布于 2021-01-19 05:54:18
我找到了一个回答。我使用unalias
消除了ga
的混叠
unalias ga
ga() {
echo "ZAWARUDO"
}
发布于 2021-01-19 00:09:40
你忘了删除旧的定义。最简单的方法就是打开一个新的交互式bash。与其采购.bashrc
,不如做一个简单的
bash
当然,这意味着函数/别名/非导出变量(您在当前shell中手动定义了)也会丢失。
https://stackoverflow.com/questions/65787149
复制相似问题