Bash – convenient way to wrap a bash command list into rlwrap

bashcommand lineline-editor

Thanks to a question on superuser.com, I found out about this utterly convenient rlwrap tool.

It satisfies my needs (i.e. add command history to another cmdline tool), but I was wondering how I can use it to add command history to a 'compound' shell command, like the prototypical

$> while read line; do echo "i read $line"; done
hi
i read hi
^D

When I put the while loop inside a shell script, and execute it like rlwrap ./whilereadline.sh, it's ok.

But I'm wondering how I can do this without the need for an additional file, somewhat like

$> rlwrap (while read line; do echo "line: $line"; done)
bash: syntax error near unexpected token `while'

Any ideas?

Best Answer

Have you tried

rlwrap sh -c 'while read line; do echo "i read $line"; done'

rlwrap needs a command it can run, which a () syntax-induced subshell is not. sh -c ... is a command however. Replace sh with bash or whatever shell you prefer.

Related Question