I have this command sequence that I'm having trouble understanding:
[me@mine ~]$ (echo 'test'; cat) | bash
echo $?
1
echo 'this is the new shell'
this is the new shell
exit
[me@mine ~]$
As far as I can understand, here is what happens:
echo 'test'
is sent to the pipe.echo $?
returns 1, which is what happens when you run test
without args. cat
runs.
bash
will execute whatever you type in, but stderr won't get printed to the screen (we used |
, not |&
). I have three questions:
It looks like, even though we run two commands, we use the same pipe and bash
process for both commands. Is that the case?
Where do the prompts go?
When something like cat uses stdin, does it take exclusive ownership of stdin as long as the shell runs, or can other things use it?
I suspect I'm missing some detail with ttys, but I'm not sure. Any help or details or man
excerpt appreciated!
So...
Yes, there's a single pipe sending commands to a single instance of bash
. Note:
$ echo 'date "+%T hello $$"; sleep 1; date "+%T world $$"' | bash
22:18:52 hello 72628
22:18:53 world 72628
There are no prompts. From the man page:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals. PS1 is set and $- includes i if bash is interactive.
So a pipe is not an interactive shell, and therefore has no prompt.
Stdin and stdout can only connect to one thing at a time. cat
will take stdin from the process that ran it (for example, your interactive shell) and send its stdout through the pipe to bash
. If you need multiple things to be able to submit to the stdin of that cat
, consider using a named pipe.
Does that cover it?