Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.