Sending CLI Output to Whatsapp using Command Line

As a software developer I’m using Linux a lot and executing lots of command in the command line. Sometimes, we required to send the output of our executed commands to our colleagues over Whatsapp. But, to do this there is no direct app available.

So, to overcome this problem I’d written one script. By using this script we can send our command line output to the Whatsapp contacts. So, let’s start writing this script.

System Requirements:

  • Any Linux Distro eg. RHEL7/8, MX Linux, Ubuntu, etc.

Step 1:

First of all, we required to log in to the Whatsapp web.

Step 2:

Create the shell script file, let’s say and add the following code to it.

# Accept the phone number of the receiver from user
read -p "Enter the phone number of receiver in international format: " phoneNumber
# Read the command required to execute from the user
read -p "Enter the command you want to execute: " rhelCommand
# Executing the command and storing command output in the variable
# Replacing space, + sign with %20
messages=`echo $message | sed 's/ /%20/g' | sed 's/+/%20/g'`
# Creating the link with phone number and command line output
echo $link

The code is here

Here, we are accepting the phone number of the receiver and command to execute from the user. Next, step is we required to execute this command and store the output of the command in a variable named message.

Here, we have one restriction that the Whatsapp link accepts text as a continuous string i.e without any space or + sign. So, we required to replace the space or + sign with %20, which is done at line no.12.

Now, we have a phone number and a text message to send. Finally, we required to create a link to send the text message to the Whatsapp contact.

The general format of the link to chat is:

where PHONENUMBER is replaced by the phone number of the receiver and MESSAGE is replaced by the message to send.

Finally, we print out the link with the phone number and message.

Step 3:

Now, we required to run this script and assign its output i.e. the link to the variable named var in the command line.

$ var=$(sh
CliToWhatsapp: Accepting input

Note: The phone number must be entered in an international format and without + sign and space. Eg. For India country code is +91. So, for India phone number must start with 91 followed by a 10-digit mobile number as 91XXXXXXXXXX.

Step 4:

The variable named var contains the appropriate link to send the CLI output. We can open this link with google chrome using the following command:

$ google-chrome $var
CliToWhatsapp: Opening Whatsapp message link with google chrome

This will open the Whatsapp web app in the Google Chrome browser, automatically opens the chat window for an entered phone number, and fills the input message-box with the command line output we had passed through the link.

CliToWhatsapp: Whatsapp chat windows with automatically filled CLI output

Step 5:

You are all done! As a final step, you are just required to click on the send button and our CLI output is sent to the contact.

Github Repo link for the code:

Thanks a lot for reading my blog! If you liked this article, please applaud it. You can also follow me on Twitter at @cankush625 or find me on LinkedIn. I’d love to hear from you if I can help you with Shell Script and automation.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ankush Chavan

Ankush Chavan

Tech blogger, researcher and integrator