writecommand 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.
- Access to the terminal.
- A multiuser environment.
- Sudo group privileges.
- A text editor to create text files.
Linux write Command SyntaxThe syntax for the
writecommand in Linux is:
write <user> <tty name>
userparameter is mandatory and represents the username of the receiving end. The
tty namespecifies the terminal environment in case of multiple open terminals.The communication requires write permissions. Enable the permission with:
The terminal does not output a message.
Linux write Command ExamplesThe
writecommand requires at least two logged-in users through a pts (pseudo terminal slave) or tty (teletype) session. Log in with:
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:
sudo login <username>
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
logincommand for both users.
Write a Message To a UserTo write a message to another user using the
writecommand, do the following:1. Run the
writecommand and specify the user:
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.
writecommand, press CTRL+D. The second user receives an
EOFmessage, indicating the command has ended.
Hold a ConversationTo hold a conversation between two users using the
Send any message after. For the second user to respond, run the
writecommand specifying the first user's name:
Since there's no proper way to distinguish when a user completes a message, the traditional way to end a statement is with an
ocharacter (symbolizing "over"). To signal the end of a conversation, write
oo(for "over and out").CTRL+D sends the
EOFinterrupt character to end the communication channel and terminates the program.
Pipe a Message To WriteUse the echo command to type a message and pipe to the
The communication channel ends immediately for the sender, while the receiver receives the message and waits for termination.
echo "Hello from KB" | write bob
Write a Message From FileTo send a message from a text file, do the following:1. Create a text file using a text editor, such as nano:
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
The receiver sees the message from the text file.
write bob < message.txt
Write to Specified TTYWhen a user has multiple terminal sessions open, the
writecommand selects the terminal with the shortest idle time.
To specify which TTY to send to, run:
Note: Use the
wcommand to check the TTY name and idle time.
The messages arrive only to the pts/0 TTY.
write bob pts/0
Write in Bash ScriptsUse
writein Bash scripts to notify yourself or another user when a command or task completes. For example, take a look at the following Bash script:
Running the script starts an update and upgrade on a system. The
#!/bin/bash sudo apt update -y echo "Update completed" | write bob sudo apt upgrade -y echo "Upgrade completed" | write bob
writecommand helps inform the user
bobwhen each step completes, printing the message to his terminal.
ConclusionAfter going through the examples in this tutorial, you know how to use the
writecommand in Linux.Next, learn how to use the Bash read command and save a user's input.