Introduction
Thewrite
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 thewrite
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
Thewrite
command requires at least two logged-in users through a pts (pseudo terminal slave) or tty (teletype) session. Log in with:sudo login <username>

w

login
command for both users.Write a Message To a User
To write a message to another user using thewrite
command, do the following:1. Run the write
command and specify the user:write bob

- Who the message is coming from.
- Where the message is coming from.
- The timestamp.

write
command, press CTRL+D.
EOF
message, indicating the command has ended.Hold a Conversation
To hold a conversation between two users using thewrite
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

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 thewrite
command:echo "Hello from KB" | write bob

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

Write to Specified TTY
When a user has multiple terminal sessions open, thewrite
command selects the terminal with the shortest idle time.Note: Use the
w
command to check the TTY name and idle time.write bob pts/0

Write in Bash Scripts
Usewrite
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 thewrite
command in Linux.Next, learn how to use the Bash read command and save a user's input.