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.
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.
echo " source destination | mvbut was not successful. As mentioned, I am very newbie to this area. However, I believe they do same job if you run these two commandsCommand B | Command Cdoes the same job asCommand D” (which you could have easily checked, in isolation, without introducingCommand A), and then you pile a complicated question on top of the initial misconception, and then you change the question halfway through (pullingxargsout of a hat), and you give it a title that’s down in the weeds of your example, rather than describing your question (such as it is), then nobody else is going to learn anything from it.