Tian Chen
Tian Chen

Reputation: 503

Call bash function in IRB

If I run a custom bash function under shell console:

~/w/dotfiles/ruby [g:master-] ¶ repository_root
/Users/tian/Documents/workspace/dotfiles

And if I run the custom bash function under IRB:

irb(main):001:0> `repository_root`
(irb):1: command not found: repository_root
=> ""

How could I get the same result in IRB?

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

Upvotes: 3

Views: 1546

Answers (3)

glenn jackman
glenn jackman

Reputation: 246744

Assuming your function is in one of your bash profile files, you want to start up an interactive (-i) login (-l) shell to execute (-c) your function:

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

Upvotes: 2

Apoorv Parijat
Apoorv Parijat

Reputation: 871

One way to do this is to make a command out of the function.

Here's a short how-to

  1. Create a shell script file that calls the function.
  2. Create a .bin directory in your HOME and add it to $PATH in .bash_rc.
  3. Place the shell script file in .bin.
  4. source .bash_rc to update the $PATH variable you just changed.
  5. Assuming you named the file fnx, just use the back tick operator or exec to run the command - exec("fnx")

Upvotes: 1

user904990
user904990

Reputation:

where is repository_root declared?

.bash_profile? .bashrc?

try to source that file before using repository_root

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

Upvotes: 0

Related Questions