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?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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