URL Shortening using CLI

Suyash Chandrakar
3 min readFeb 7, 2025

--

Image Suyash Chandrakar

In today’s fast-paced digital world, sharing long and cumbersome URLs can be a hassle. Whether you’re sharing links on social media, in emails, or even in your code, a shortened URL can make your life easier.

URL shortening not only makes links more manageable but also improves readability and aesthetics.

In this article, we’ll explore how to shorten URLs using a Command Line Interface (CLI) with a practical code example.

This guide is optimized for SEO, ensuring you get the most out of your reading experience.

Why Use a CLI for URL Shortening?

Using a CLI for URL shortening offers several advantages:

  1. Automation: Easily integrate URL shortening into scripts or workflows.
  2. Speed: Quickly shorten URLs without opening a browser.
  3. Customization: Tailor the process to your specific needs.
  4. Privacy: Avoid using third-party web services that may track your activity.

How to Shorten URLs Using CLI

To shorten URLs via CLI, we’ll use a popular URL shortening service like Bitly or TinyURL. For this example, we’ll use Bitly’s API. You’ll need a Bitly account and an API token to get started.

Step 1: Get Your Bitly API Token

  1. Sign up for a Bitly account at bitly.
  2. Generate an API token from your account settings.

Step 2: Install Required Tools

Ensure you have curl installed on your system. Most Linux and macOS systems come with curl pre-installed.

For Windows, you can download it from curl.

Step 3: Write the CLI Script

Here’s a simple Bash script to shorten a URL using Bitly’s API:

#!/bin/bash

# Replace with your Bitly API token
API_TOKEN="your_bitly_api_token_here"

# Check if a URL is provided
if [ -z "$1" ]; then
echo "Usage: $0 <URL>"
exit 1
fi

# URL to shorten
LONG_URL=$1

# Shorten the URL using Bitly API
SHORT_URL=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "{\"long_url\": \"$LONG_URL\", \"domain\": \"bit.ly\"}" \
https://api-ssl.bitly.com/v4/shorten | grep -oP '"id":"\K[^"]+')

# Output the shortened URL
if [ -z "$SHORT_URL" ]; then
echo "Error: Failed to shorten the URL."
else
echo "Shortened URL: $SHORT_URL"

Step 4: Run the Script

  1. Save the script as shorten_url.sh.
  2. Make it executable:
chmod +x shorten_url.sh

3. Run the script with a long URL as an argument:

./shorten_url.sh "https://www.example.com/very/long/url/to/shorten"

Example Output

Shortened URL: https://bit.ly/3xYzABC

SEO Benefits of URL Shortening

Using shortened URLs can indirectly boost your SEO efforts:

  1. Improved User Experience: Short URLs are easier to share and remember, leading to higher click-through rates.
  2. Tracking and Analytics: Services like Bitly provide analytics, helping you understand user behavior.
  3. Cleaner Links: Shortened URLs look cleaner in social media posts, emails, and other platforms, encouraging engagement.

Best Practices for URL Shortening

  1. Use Reliable Services: Stick to trusted URL shortening services like Bitly or TinyURL to avoid broken links.
  2. Monitor Performance: Regularly check analytics to see how your shortened URLs are performing.
  3. Avoid Overuse: Don’t shorten URLs unnecessarily, especially on your own website, as it can confuse users and search engines.

Conclusion

URL shortening using a CLI is a powerful tool for developers, marketers, and anyone who frequently shares links. By automating the process, you can save time and improve efficiency.

The provided Bash script demonstrates how easy it is to integrate URL shortening into your workflow using Bitly’s API.

Give it a try and see how it can benefit your workflow!

--

--

Suyash Chandrakar
Suyash Chandrakar

No responses yet