Skip to main content
1 of 4

piping into results of another pipe not working as expected. Command A | {Command B | Command C;}

I am a newbie in bash programming and was playing around with some commands trying to learn how passing multiple arguments using pipeline and grouping commands work that I faced this odd behavior which confused me a lot. I know other ways to achieve the what I want, however, I am trying to understand why this is happening.

Abstract

What confuses me is that why Command A | { Command B | Command C; } does not the same job as Command A | Command D where Command B | Command C does the same job as Command D. Where:

Command A = $ echo "some message"

Command B = $ echo "USER TTY"

Command C = $ write

Command D = $ write USER TTY

Details

I am trying to send a message to a user ( let's consider its user ID as USER and it TTY as TTY) connected to my SSH server, using write builtin function.

While I was able to send the message just fine using the following command:

$ echo "some message" | write USER TTY

But when I was trying to pass USER and TTY using another pipe line, the message was not being sent :

$ echo "some message" | { echo "USER TTY" | xargs -o write; }

In the results, it seems the bash ignores the first part ($ echo "some message"), and need to enter the message after execution of the command.

Note that { echo "USER TTY" | xargs -o write; } and write USER TTY does the same job (apparently? I suspect there is a difference here that I am not aware of)

Again, I know that there are much easier ways to do this, but I just am trying to understand how bash works regarding grouping commands, piping, and passing input arguments to a function. Any comments on these areas are extremely appreciated.

PS: Regarding what I mentioned in the Title, in my example:

Thanks.