在系统(OSX)中注册自定义shell功能

luk*_*ups 13 bash shell function .bash-profile

今天我想编写一个函数,将我的博客页面(以nanoc开发)自动部署到github页面 - 这是脚本:

function cmit()
{
  nanoc compile;
  git add .;
  git commit -am $1;
  git push origin source;
  cd output; git add .;
  git commit -am $1;
  git push origin master;
  cd ..;
  echo 'new version deployed! now starting nanoc locally..';
  nanoc aco;
}
Run Code Online (Sandbox Code Playgroud)

用法示例: cmit "my example commit!"

我真的不知道如何在系统(OSX)注册我的功能-在.bashrc,.bash_profile也许别的地方?请帮忙!

Chr*_*our 16

只需将其添加到~/.bashrc文件的底部,然后您就可以cmit像常规命令一样使用,您需要刷新当前的shell以获取更改以便运行source ~/.bashrc.如果您将该功能保存在文件中,则cmit只需cat cmit >> ~/.bashrc将该功能附加到您的文件末尾即可~/.bashrc.

您可以先尝试一下测试功能:

# add to ~/.bashrc first
function test() {
    echo "Some test foo!"
}

$ source ~/.bashrc

$ test
Some test foo!
Run Code Online (Sandbox Code Playgroud)

  • 似乎在OSX上使用`〜/ .bashprofile`代替`〜.bashrc`,所以把函数放在`〜/ .bashprofile`中,应该解决它. (2认同)