Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

This happens on every distro I've tried:

$ cat | bash -i
bash-3.2$ ls
foo bar

And now the session is hung. Ie, you get one command and then you pretty much have to close the terminal window. SIGTERM (aka ^C) is caught by bash but it won't get you back to a working shell.

I'm guessing it has something to do with the -i causing bash to fight with cat for control of the tty, but I can't find anything definitive. Can anyone explain what's going on? And how do I automate input to a program that's running execlp("/bin/bash", "bash", "-i") without hanging? Do I have to write an expect-like program that handles tty vagaries?

share|improve this question
up vote 4 down vote accepted

The short answer: yes, you're going to have to use Expect.

As for why it's failing: based on its behavior, bash -i turns on readline (even with --noediting), which plays rather badly with pipes. It also appears to be setting terminal modes (including non-blocking mode) via stdout instead of stdin, which means you lose the expected behavior of most control characters. (It is not, however, reading the tty directly.)

Side note: ^C sends SIGINT, not SIGTERM. Both are, however, trapped in interactive (-i) mode; SIGHUP works to kill it.

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.