我想为带有标志的特定命令创建别名
arc diff --preview
我创建了以下bash函数
arc(){
if [ "$1" = "diff" ] && [ "$2" = "--preview" ] ; then
echo -n "Message example? (y/n)? "
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
arc $@
else
echo No
fi
else
echo "$2"
fi
}
不幸的是,在说“是”或输入其他命令(例如arc pull
)之后
我正在输入相同的提示"Message example? (y/n)? "
,因为我再次呼叫{{1} }。
我该如何避免呢?
答案 0 :(得分:2)
您正在再次调用同一函数。如果arc是外部命令,则执行command arc $@
。这样可以避免调用您定义的函数,并避免在PATH中找到外部可执行文件。
arc(){
if [ "$1" = "diff" ] && [ "$2" = "--preview" ] ; then
echo -n "Message example? (y/n)? "
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
command arc "$@"
else
echo No
fi
else
echo "$2"
fi
}