Take the 2-minute tour ×
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.

This happens on every distro I've tried:

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

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.