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
i read hi

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?

2 Answers 2


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.


rlwrap needs a program to execute, it can't interpret the commands itself. You can wrap the shell code you want in a bash -c command:

rlwrap bash -c 'while read line; do echo "line: $line"; done'

Note that depending on what code you want to use for this, the quoting could become somewhat ugly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .