有没有一种好方法可以将命令别名cp file1 file2
为cp -i file1 file2
?
Ant*_*hon 12
您应该在启动脚本中添加别名:
alias cp='cp -i'
Run Code Online (Sandbox Code Playgroud)
你可以直接把它放在~/.bashrc
,但我有我的~/.bashrc
:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Run Code Online (Sandbox Code Playgroud)
和~/.bash_aliases
我有:
alias realias='source ~/.bash_aliases'
alias cp='cp -i'
alias rm='rm -i'
Run Code Online (Sandbox Code Playgroud)
并且当我向该文件添加/更改内容时,我会这样做realias
(这不会从您取出的正在运行的 shell 中删除别名,以便使用unalias
)。
如果您这样做man bash
并搜索别名,您将找不到示例,但是:
For almost every purpose, aliases are superseded by shell functions
The (`bash`) shell function alternative for the above alias is:
cp () { command cp -i "$@" ; }
Run Code Online (Sandbox Code Playgroud)
shell 函数更强大,但对于简单的事情,别名就足够了。
我仍然倾向于使用它们。