Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby exec use functions from my ~/.bashrc

Tags:

bash

ruby

exec

In my ~/.bashrc, I have a function:

function sayHi() {
    echo "hi, $@"
}

and in Ruby I want to:

`sayHi "friend"`

however, sayHi is "not found" in whatever context ruby runs exec or system.

How could I get Ruby to use functions in my .bashrc?

like image 612
tester Avatar asked Jan 28 '26 09:01

tester


1 Answers

Your .bashrc is only run for interactive shells. When you execute a command using backquotes in Ruby, it does not execute the command in an interactive shell.

You can force an interactive shell by running bash -i. For example, to run your command under an interactive shell, use:

`bash -ic 'sayHi "friend"'`
like image 121
Brian Campbell Avatar answered Jan 30 '26 01:01

Brian Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!