-   Programming (
-   -   bash script with own command line interpreter (

Juako 01-16-2011 02:24 AM

bash script with own command line interpreter
update: i'm changing this post to a question, i really need help to properly implement TAB completion to make this work (it's part of a bigger framework). If anyone has info on how to get readline+complete/compgen to work when using "read -e", please post it!!!

original post:

Hello, this is a working template for an embedded command interpreter in a bash script. Given the scarce material i've seen on this i'm posting as a time saver for everyone.

It needs bash 4 with readline to work properly. Has command line edition, history and tab completion (provided by readline).

It also has automatic listing and validation of user commands based on the functions defined. If you want a function to NOT be accesible to the user, just use a "_" as the first character of its name (of course, this can be altered as needed).

i invite you to use/expand/comment etc.



# funcs ------------------------------------------------------------------------
cmd1() {
    echo $FUNCNAME: "$@"

cmd2() {
    echo $FUNCNAME: "$@"

cmd3() {
    echo $FUNCNAME: "$@"

_complete() {
    echo $allowed_commands

# start ------------------------------------------------------------------------
shopt -qs extglob

set -o emacs

bind 'set show-all-if-ambiguous on'
bind 'set completion-ignore-case on'
bind 'TAB:dynamic-complete-history'

unset -v HISTFILE
allowed_commands="$(declare -f | sed -ne '/^[^_].* () /s/ ().*//p' | tr '\n' ' ')"
for command in $allowed_commands ; do
    history -s $command

echo "waiting for commands"
while read -ep"-> "; do
    history -s $REPLY
    case "$REPLY" in
        @(${allowed_commands// /|})?(+([[:space:]])*)) $REPLY ;;
        \?) _complete ;;
        *) echo "invalid command: $REPLY" ;;

Juako 01-16-2011 02:39 AM

Forgot to mention that the technique to get TAB completion is pulled from another LQ thread, thank you!

Anyway, i was hoping to get completion to work with the standard complete/compgen builtins instead of this history hack, but i couldn't for the life of me get those to work in the script whatsoever. I'd very,very much like to hear something about this working, as AFAICS it's truly an ugly part.

In this other LQ thread "complete" is reported as working, though it didn't worked for me.

The other bit i'd like to get some help about is this command substitution:


allowed_commands="$(declare -f | sed -ne '/^[^_].* () /s/ ().*//p' | tr '\n' ' ')"
The last pipe (that of "tr") seems redundant. I know sed has the ability to transform the newlines to spaces...hopefully some sed warrior sheds some light here.

Juako 01-19-2011 05:47 PM

As detailed in this sister thread, programmable completion in read is disabled in the bash code by the maintainer.

I have contacted the maintainer of Bash, and been toold that this use case (presenting commands to the end user for completion) is the only one presented for programmable completion in the context of read, and it isn't considered compelling enough to worth the effort of implementing it.

This answer left me highly unsatisfied. I do not concord at all with that view, in fact i think that since you have a functionality that is clearly oriented to provide CLI implementations inside bash scripts, it well worths the effort to have it complete.

All times are GMT -5. The time now is 01:54 AM.