tomerfiliba / plumbum

Plumbum: Shell Combinators
https://plumbum.readthedocs.io
MIT License
2.8k stars 182 forks source link

Is it possible to call bash functions? #502

Open nkabir opened 4 years ago

nkabir commented 4 years ago

Does plumbum support accessing bash functions in addition to bash commands?

kreczko commented 3 years ago

Also stumbled across this today. The function is available and callable from the terminal, but when trying to call it with plumbum I get a CommandNotFound error.

A good example is declare (bash builtin):

which declare
/usr/bin/which: no declare in 
declare X=Y
echo $X
>> Y
from plumbum import local
declare = local["declare"]

plumbum.commands.processes.CommandNotFound: ('declare', ....

The limiting code is in https://github.com/tomerfiliba/plumbum/blob/master/plumbum/machines/base.py#L51

For bash functions to work, one would probably need to introduce a new Path specialization.

nkabir commented 2 years ago

A workaround is to use a dispatch shell script (e.g. dispatch.sh):

#!/usr/bin/env bash

readonly TARGET_FUNCTION="${1:?missing target function}"
shift
eval ${TARGET_FUNCTION:?} $@

Then

>>> dispatch = local["./dispatch.sh"]
>>> dispatch["mybash_func"].run_fg()