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?

  • 1
    I didn't reproduced after many tries, but finally made it with echo hi > >(sleep 1; cat). I guess the race was not fair on my box. – thanasisp May 09 '22 at 23:05
  • 1
    See: https://unix.stackexchange.com/questions/403783/the-process-substitution-output-is-out-of-the-order and https://unix.stackexchange.com/questions/351780/wait-for-bash-process-substitution-subshells – thanasisp May 09 '22 at 23:06

1 Answers1

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.

AdminBee
  • 22,803
glenn jackman
  • 85,964