Generating a QR code with Bash

Generating a QR Code directly from a Bash shell is a simple and useful way to automate the sharing of URLs, text, or Wi-Fi credentials. In this guide, we’ll see how to do it using qrencode, a command-line tool.

Requirements

To generate a QR Code from Bash, you need to install the qrencode package. On a Debian-based distribution (like Ubuntu), you can install it with:

sudo apt update
sudo apt install qrencode

On Arch Linux systems:

sudo pacman -S qrencode

And on macOS with Homebrew:

brew install qrencode

Generating a QR Code as a PNG Image

To generate a QR Code representing a string (for example, a URL) and save it as a PNG image, you can use the following command:

qrencode -o qrcode.png "https://example.com"

This will create a PNG file called qrcode.png containing the QR Code for the provided link.

Displaying a QR Code in the Terminal

It’s also possible to display the QR Code directly in the terminal, without generating an image file:

qrencode -t ANSIUTF8 "https://example.com"

The -t flag specifies the output type, and ANSIUTF8 means it will be printed using characters compatible with most terminals.

Using in a Bash Script

You can include QR Code generation in a Bash script to automate the process:

#!/bin/bash

read -p "Enter the text to convert into a QR Code: " input
filename="output.png"

qrencode -o "$filename" "$input"

echo "QR Code saved as $filename"

Save this script as generate_qr.sh, make it executable with chmod +x generate_qr.sh, and run it to generate custom QR Codes.

Conclusion

With just a few commands and a lightweight tool like qrencode, you can easily generate QR Codes from any Bash terminal. It’s a practical method for quickly sharing information in a visual format.

Back to top