The write command in Linux allows two logged-in users to communicate with each other via the terminal in real time until they are on the same network.
This command copies the text from one terminal to another, making it possible to send messages or text content with or without piping.
Note that both parties need to have terminal write access turned on before they can talk to each other. If you don’t know what this means, check out our mesg command article.
So, let’s keep this aside and see how you can send message or text file content to another user with or without piping using the write command in Linux.
|Description||Write a Message on the Terminal of a Specified Logged in User.|
|Root or Sudo Privileges||No|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
Usage of the Write Command
The write command takes two arguments: one is the user login name (required), and the other is the TTY name (optional).
$ write [USERNAME] [TTY Name]
Note that the user’s login name (or account name) needs to be given when this command is invoked.
The TTY name is only needed if the same user logs in to the same network using more than one method, such as multiple terminal windows or an SSH session.
To make sure both terminals (sender and receiver) can talk to each other, use the following command to check that the terminal’s write access is enabled.
From the above picture, it’s clear the current terminal has write access; in access, if the output is “
is n“, then execute the “
mesg y” command to enable terminal write access.
How to Check All Users on the Same Network
Before using the write command, you need to find out all the users on the same network and their account login names.
To check who logged in, use the w command, which will return all the user’s login names as well as their tty (teletype) or pts (pseudo terminal slave).
As you can see from the above picture, three users are connected to the same network, and “
david” logged in multiple times with two different SSH sessions.
In this scenario, where the same user is connected with two different methods, they will only receive the message on the active one; however, you can specify their TTY to send the message on a specific terminal line.
How to Write a Message to a User
The simplest way to use this command is to directly invoke it, specifying the user’s login name as an argument.
$ write david
As you can see from the above picture, when the command is invoked in the sender system, it expects the input message that will be sent to another user (or receiver).
While on the receiver end, it will show the information related to the sender in the following order:
linuxtldr@linux– username@hostname of sender.
pts/1– Sender TTY.
15:14– The timestamp.
Now both users can type the message and press the “Enter” key to send it to each other.
To end the communication, simply use the “Ctrl+D” shortcut key.
As you can see from the above picture, once the user terminates the communication, the receiver will receive the EOF (end-of-file) message on the screen, indicating that the conversation is over.
How to Write a Message on a User’s Specific TTY
When a user has more than one terminal session open, the message will be sent to the one that is currently active.
However, you can send the message to the user’s specific session by passing their TTY as an argument to this command.
w” or “
who” commands to get all the connected user information.
$ write david pts/2
How to Write a Message from a Text File
If you have a text file, then you can send the content of it as a message to another user using the write command.
For example, the following is “
file.txt” content that you can use as an example.
$ cat file.txt This is the "file.txt" For the write command example Bye bye!
Now you can copy and paste the above content in a text file and pass the file name as an argument to the write command with the “
<” redirection symbol.
$ write david < file.txt
Once the text file content has been sent to another user, the communication will automatically end.
How to Write a Message by Pipe
In contrast to the previous example, you can pipe another command and send its output to another user’s terminal as a message.
In the following example, the echo command is piped with the write command, and the output of the echo command will be sent to the specified user as a message.
$ echo "It's Linux TLDR" | write david
Write in Bash Script
You can use the write command to notify yourself when a specific or a group of commands in the shell script are executed.
For example, the following command will notify the “
linuxtldr” user when the system update and upgrade are done.
#!/bin/bash sudo apt update -y echo "Update completed" | write linuxtldr sudo apt upgrade -y echo "Upgrade completed" | write linuxtldr
Note that the current user should not be equal to the user mentioned in the shell script.
So, that is all for now.
I hope this article is useful for you and you learned something in your Linux journey.
However, if you are stuck somewhere or have any questions or queries related to this or anything else, then feel free to ask them in the comment section.
Till then, peace!