write Command in Linux With Examples

July 13, 2022

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.

write Command in Linux With 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>
sudo login terminal output

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
w command two user pts terminal output

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
write command connection terminal output

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.

write command message terminal output

The same message appears on the receiving end in real-time.

3. To end the write command, press CTRL+D.

write eof terminal output

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
write conversation terminal output

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
write echo pipe message terminal output

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
write from text file terminal output

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
write TTY terminal output

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.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How To Use The Bash read Command
February 21, 2022

The Bash read command reads a user's input or a file descriptor and allows splitting into different fields. Learn how to utilize...
Read more
Linux expect Command With Examples
June 21, 2022

Up your automation game with the expect command and automate responses to scripts and programs. Learn how to use Expect...
Read more
How to Use the Linux history Command
March 3, 2022

the Linux bash shell saves a command history list, allowing you to review and reuse previous commands. Learn how to manage this list using the Linux history...
Read more
Bash trap Command Explained
December 16, 2021

A Bash shell script can run into problems during its execution, resulting in an error signal that interrupts the script unexpectedly. This tutorial will show you how to use the...
Read more