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 when 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=writeCommand D=write USER TTY
Details
I am trying to send a message to a user (let's consider their user ID as USER and their TTY as TTY) connected to my SSH server, using the write
builtin function program.
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 pipeline, 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.
For those who suspect I am asking this for a homework, which is nice to see people caring about these stuff, I genuinely was trying to create an alias that sends a message to every user on my ssh server, which I found wall does it way easier, though found it interesting to figure what I mentioned here out.