printf "REPL for %s\n" "$@"
[ $# -gt 0 ]
while true ; do
printf "%s> " "$@"
read -r || break;
notblank $REPLY || continue;
eval command \"\$@\" "$REPLY"
If $REPLY expands to nothing, including multiple whitespace, then we just want to print the prompt again: not quit and not run the command with no additional arguments.
The eval trick allows $REPLY to undergo expansion and splitting, so that shell syntax can freely be used in the REPL.
$ ~/test/replify/replify.sh git
REPL for git
git> rev-parse HEAD
git> branch -r
origin/HEAD -> origin/master
git> checkout "$TERM $TERM"
error: pathspec 'xterm xterm' did not match any file(s) known to git.
git> checkout $TERM
error: pathspec 'xterm' did not match any file(s) known to git.
Submit a patch for Bash to do this, and maybe you have something. Bash has a hook feature for command execution, IIRC, so this may be somehow doable without modifying Bash.
EDIT: beat to the punch, should've refreshed.
to do so however didn't just require a similar number of lines, it actually required 4 more (though to be fair, both scripts have comments and a fair bit of formatting, so it may not be a fair comparison. also it probably doesn't need stating, but i'm a bit of a hack, so my implementation maybe shouldn't be the reference point. but i digress)
if you remain convinced that the only correct language for such a tool is bash/shell, i imagine OP certainly welcomes PRs; i know i do.
afaik, it will work just about anywhere you can invoke a shell (i haven't tested it all that extensively)