4

I'm a new Linux user and I was doing some experiments and trying to understand Process Substitution. I believe I already have a basic understanding of it. But here is a case that I don't know why. I'm using Bash on Ubuntu 20.04.

echo hi just sends the string hi with a newline character to stdout.

root@u2004:~# echo hi | od -a
0000000   h   i  nl
0000003
root@u2004:~#

cat can read from the pipeline as its stdin, and send what it read down to the pipeline.

root@u2004:~# echo hi | cat
hi
root@u2004:~# echo hi | cat | od -a
0000000   h   i  nl
0000003
root@u2004:~#

As I understand, the command echo hi > >(cat) will also make cat read from its stdin (and print to the console). But when I run it, I got this:

root@u2004:~# echo hi > >(cat)
root@u2004:~# hi
<the cursor blinks at this location>

I have to press the Enter key to finish the command.

root@u2004:~# echo hi > >(cat)
root@u2004:~# hi
Enter
root@u2004:~#

Why this behavior? Is my understanding of the command echo hi > >(cat) correct?

2

1 Answer 1

6

Nothing is wrong, the command completed successfully.

What you saw:

root@u2004:~# echo hi > >(cat)
root@u2004:~# hi
<the cursor blinks at this location>

You entered the command, hit Enter. Your shell printed out your prompt, and then the cat process, launched asynchronously, completes and spits out hi\n.

The cursor blinking on the blank line belongs to the prompt on the previous line, but the newline from the echo command has disrupted your display.

The subsequent "hitting enter" just gives you a new prompt. If you enter a command and hit enter, that command will run.

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