Introduction
The write
command in Linux creates a communication line between two logged-in users through the terminal. The command allows sending messages between users in real-time by copying text from one terminal to another.
This article shows how to use the write command through examples.
Prerequisites
- Access to the terminal.
- A multiuser environment.
- Sudo group privileges.
- A text editor to create text files.
Linux write Command Syntax
The syntax for the write
command in Linux is:
write <user> <tty name>
The user
parameter is mandatory and represents the username of the receiving end. The tty name
specifies the terminal environment in case of multiple open terminals.
The communication requires write permissions. Enable the permission with:
mesg y
The terminal does not output a message.
Linux write Command Examples
The write
command requires at least two logged-in users through a pts (pseudo terminal slave) or tty (teletype) session. Log in with:
sudo login <username>
For example, if working on the same machine with two different users, each user runs the command in their terminal session and provides their username.
The command prompts to enter the sudo and the user's password to continue.
To check who is logged in, use the w command in either terminal as follows:
w
The output shows two users logged in through a pts console, whereas the first is logged in through a local display (GUI). The first user ran the login
command for both users.
Write a Message To a User
To write a message to another user using the write
command, do the following:
1. Run the write
command and specify the user:
write bob
The command expects the input message. On the receiving end, the user gets confirmation about a connection with the following information:
- Who the message is coming from.
- Where the message is coming from.
- The timestamp.
2. Write a message to the user. Pressing Enter goes into a new line and does not end the message.
The same message appears on the receiving end in real-time.
3. To end the write
command, press CTRL+D.
The second user receives an EOF
message, indicating the command has ended.
Hold a Conversation
To hold a conversation between two users using the write
command, run:
write bob
Send any message after. For the second user to respond, run the write
command specifying the first user's name:
write kb
Since there's no proper way to distinguish when a user completes a message, the traditional way to end a statement is with an o
character (symbolizing "over"). To signal the end of a conversation, write oo
(for "over and out").
CTRL+D sends the EOF
interrupt character to end the communication channel and terminates the program.
Pipe a Message To Write
Use the echo command to type a message and pipe to the write
command:
echo "Hello from KB" | write bob
The communication channel ends immediately for the sender, while the receiver receives the message and waits for termination.
Write a Message From File
To send a message from a text file, do the following:
1. Create a text file using a text editor, such as nano:
nano message.txt
2. Add some text to the file.
3. Save the file and close nano (CTRL+X, Y, Enter).
4. Send the file's contents through the write
command with:
write bob < message.txt
The receiver sees the message from the text file.
Write to Specified TTY
When a user has multiple terminal sessions open, the write
command selects the terminal with the shortest idle time.
Note: Use the w
command to check the TTY name and idle time.
To specify which TTY to send to, run:
write bob pts/0
The messages arrive only to the pts/0 TTY.
Write in Bash Scripts
Use write
in Bash scripts to notify yourself or another user when a command or task completes. For example, take a look at the following Bash script:
#!/bin/bash
sudo apt update -y
echo "Update completed" | write bob
sudo apt upgrade -y
echo "Upgrade completed" | write bob
Running the script starts an update and upgrade on a system. The write
command helps inform the user bob
when each step completes, printing the message to his terminal.
Conclusion
After going through the examples in this tutorial, you know how to use the write
command in Linux.
Next, learn how to use the Bash read command and save a user's input.