Ruby 在IRB中调用bash函数

Ruby 在IRB中调用bash函数,ruby,bash,Ruby,Bash,如果在shell控制台下运行自定义bash函数: ~/w/dotfiles/ruby [g:master-] ¶ repository_root /Users/tian/Documents/workspace/dotfiles 如果我在IRB下运行自定义bash函数: irb(main):001:0> `repository_root` (irb):1: command not found: repository_root => "" 我怎样才能在IRB中得到相同的结果 # de

如果在shell控制台下运行自定义bash函数:

~/w/dotfiles/ruby [g:master-] ¶ repository_root
/Users/tian/Documents/workspace/dotfiles
如果我在IRB下运行自定义bash函数:

irb(main):001:0> `repository_root`
(irb):1: command not found: repository_root
=> ""
我怎样才能在IRB中得到相同的结果

# declare
repository_root () {
    if git_is_repository ; then
        git_show_repository_root
    fi
}

repository\u root
在哪里声明

.bash_profile。bashrc

在使用
repository\u root

`. /path/to/file/declaring/repository_root; repository_root`

一种方法是从函数中发出命令

这里有一个简短的指导

  • 创建一个调用函数的shell脚本文件
  • 主页中创建
    .bin
    目录,并将其添加到
    .bash\u rc
    中的
    $PATH
  • 将shell脚本文件放在
    .bin
  • source.bash\u rc
    更新刚刚更改的
    $PATH
    变量
  • 假设您将文件命名为
    fnx
    ,只需使用back tick操作符或
    exec
    即可运行 命令-
    exec(“fnx”)

  • 假设您的函数位于一个bash概要文件中,您希望启动一个交互式(
    -i
    )登录(
    -l
    )shell来执行(
    -c
    )您的函数:

    output = %x{bash -lic 'repository_root'}
    

    它可以工作,但有没有更好的方法不需要源文件?不知道为什么它没有在第一时间运行。事实上,这是一个更好的方式。在没有任何问题的情况下对我有效。我现在就开始研究-lic开关。