How to Send Messages to a Logged-In User in Bash
This article explores methods to send data to another logged-in user in Bash. The article discusses ways to find active users and send messages to them.
Find Online Users in Bash
Before sending data to online users, you must verify whether the user is online. You can use any of the commands below to get online users of the system.
w
command: displays who is logged on and what they are doing.id
with-un
flag: when combined with the-u
and-n
flags, theid
command displays effective usernames.who
witha
flag: displays all the logged-in users.
For the sake of demonstration, let’s look at the output for the who -a
command.
root@abdullah:/home/abdullah# abdullah
pts/0 2022-01-08 12:56 (:0.0)
root@abdullah:/home/abdullah# root
pts/1 2022-01-08 12:56 (:0.0)
The pts/0
is a special file for the first terminal (the second output line). Data written to this console will be displayed on the first terminal.
For further information on the commands above, use the commands below.
man w
man id
man who
The man pages (short form of Manual Pages) is a Unix utility that displays information on system calls, library calls, formal standards and conventions, and even abstract concepts. As the man pages are downloaded alongside the kernel, they require no internet connection.
Write to an Online User in Bash
Now that we’ve explored ways to find logged users, we’ll examine ways to send messages to them.
The command below will show you how to send a message to an online user over a different terminal.
root@abdullah:/home/abdullah# write abdullah pts/0
The command above writes a message to the user abdullah
on console pts/0
. Once you type in the command above, the terminal will ask you for a message prompt.
Type that message (assume we type: Hello Abdullah
) and press CTRL+Z to exit the prompt.
Now, if you see the terminal of the other logged-in user, abdullah
, it will have the following contents:
abdullah@abdullah:~$ Message from root@abdullah on pts/1 at 12:20...
abdullah@abdullah:~$ Hello Abdullah