我有一个bash脚本,我想作为命令全局运行。为此,我将脚本移动到/usr/local/bin/somefile.sh
。
现在我仍然需要调用一个名为somefile.sh
的命令。我想为这个命令设一个别名(例如我只想用命令sf
调用脚本)。
我怎么做?
并补充@Kent的回答:
alias sf='/usr/local/bin/somefile.sh'
使用别名
ln -s somefile.sh /usr/local/bin/sf
使用软链接
mv /usr/local/bin/somefile.sh /usr/local/bin/sf
使用重命名
有不同的方式:
alias
,man alias
了解详情sf
的软链接到您的真实脚本打开位于.bashrc
的~/.bashrc
文件。每次登录系统时都会读取.bashrc
文件。将以下行添加到文件末尾。
alias sf='/usr/local/bin/somefile.sh'
然后重新登录或运行source .bashrc
如果您使用的是GNU系统,则可以使用“替代”系统:
dir=/usr/local/bin
sudo update-alternatives --install $dir/sf somefile $dir/somefile.sh 10
这将创建2个符号链接:
/usr/local/bin/sf -> /etc/alternatives/somefile
/etc/alternatives/somefile -> /usr/local/bin/somefile.sh