I have bash functions foo and bar in my ~/.bashrc.
Function foo calls an external command ext_command that itself takes as one of its arguments another command. I want to pass bar as that command, i.e. I'd want my .bashrc to look something like this:
bar() {
...
}
foo() {
ext_command --invoke bar
}
However, this doesn't work, because the external command, which is not a shell script, doesn't know bar. How can I solve this?
I was thinking to instead do
ext_command --invoke "bash -c 'bar'"
But the Bash in this invocation isn't run as an interactive shell, so it doesn't know bar either.
Hence, I believe one way to solve my problem would be to force Bash to be run as an interactive shell; unfortunately I don't know how to do that.
Another way that I would have thought should definitely work is to use
ext_command --invoke "bash -c 'source ~/.bashrc; bar'"
but for some reason this doesn't work and indeed simply running
bash -c 'source ~/.bashrc; bar'
in an interactive bash session gives
bash: bar: command not found
In any case, I don't like that solution, because I'd like foo to work no matter which file it is sourced from.