I have a bash script listening on a pipe for commands. When it sees a command it runs it. However, I would like it to support the "sideloading" of bash functions that can be called at a later time.
Here are some input/output examples to give you an idea of what the script should be doing:
# left of the arrow is input, right of arrow is output
"echo 'test'" -> "test"
"_a() { echo 'test' }" -> ""
"_a" -> "test"
I have it working fine for normal commands, however am running into issues with trying to load functions. I suspect this is because I am executing user input in a subshell, so any function defined won't be available to the parent for future commands. I have tried using a file descriptor to store the data in (stdout
and stderr
of the eval
), but could never get it working. I currently have something like this:
exec 3>&1
eval 'echo "Hello World"' >&3
read -u 3 var
exec 3>&-
echo "$var"
Is this possible?
eval
ing it, and want to keep theeval
ed state after getting the output? – muru Mar 24 '24 at 01:33