QR Code Generation on Terminal

How to encode a string into QR Code directly using the command line

There was a time when I need to transfer data from my MacBook Pro to my iPhone. There are many ways to transfer the data.

But one time, my Mac and my iPhone are on a different network in the same place.

I need to use an ssh client from my iPhone. But the iOS app unable to generate ssh keys. So I need to create the key from my MacBook Pro.

So how do I transfer the keys to my iPhone from my MacBook?

Do I need to use a cloud service like Dropbox? Sure I could, but my iPhone has no Dropbox client. I had to install it first. That’s just too many steps to follow.

Should I set up an SFTP server? Also too much work for one-time transfer.

So how can I transfer this particular text content without using any network interfaces?

That’s when I thought: “Oh, my iPhone has a camera. Why don’t I generate a QR Code and scan it.”

That’s where I began my search and found qrencode.

This command-line utility is so awesome it can generate any data into QR Code.

It can generate image files. It can also generate a QR Code directly on your terminal with UTF8 or ASCII encoding.

You can install using your favorite package manager.

$ brew install qrencode

For example, let’s say I want to transfer my public ssh key from MacBook.

$ cat ~/.ssh/id_rsa.pub | qrencode -t UTF8
█████████████████████████████████████████████████████████████████████████████
█████████████████████████████████████████████████████████████████████████████
████ ▄▄▄▄▄ █▄▀▄ █ ▀▄█▀ ▄ ▀ ▄▀▄ █▀ ▄▄██▄█▄▄ ▄█ ▄ ▄▄▄█▀▄  █▄▀▄▀█▄▄▄█ ▄▄▄▄▄ ████
████ █   █ ██  ▄█ ▄▀▀▀█▄▀▀ ▄███▀▀▀▀▄  ▀▀ ▄▄▄▀ ▀▀█ ▀ █ █  ██▀  ▄█▀█ █   █ ████
████ █▄▄▄█ ████▀▀ █▀▀█▄█▄█▀▄▄▄  ▀▀▄  ▄▄▄ ██▄▀▄ █▄ ▀▄ ▄▄▀██ ▄█  █▄█ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ ▀ █▄▀▄▀▄█ ▀▄▀▄█ █▄▀ ▀ ▀ █▄█ █ █▄▀▄▀ █▄█ ▀ ▀▄▀ ▀▄█ ▀ █▄▄▄▄▄▄▄████
████▄▄▀  ▀▄██ ▀  ▀▀▀▄   ▀  ▀▄▄ ▀▄     ▄  ▄▀ ▄█     █▀▀ ▀▀█▄▄▀▀█ ▀█▄█▀██▀ ████
████ ▀▀▄█▀▄  ▀▀ ▀ ▀▀▀█▄  █▀▄██ ▄▄▀ ▄▄   ▀▀█▀▀█▄█▄▀ █ ▀ ▀▄▄▀▀▄█▀▄█▄▄▀▄▀ █ ████
█████▀▀█▄ ▄▀▄█ ██ ▄▀▀▀   ▀  ▄▄▀▄█▄▀▀▄█▄▀▀▄ ▄█ ▀▀▀  █▀▀ ▄█▀▀▀▀▀▀▀ ██▄▀▄  ▄████
█████▀█   ▄▀█▀▀▀█▄ ▄▄▄██ ▄ ▄█ ▀ ▀▄█▀▀▄ ▄▄▀█ ▀ ▄ ▄▀█▀  ██▄██▀▀█▄█ █▀█  ▀▄▄████
████ ▄▀ ▀▀▄▀▀▀█▄▄ ▄▀ ▀▀▀ ▀ ▄ ▀▄█▀█ █▄▀▀█   ▀█▄    █ █▀ █ ▄█▀▄   ▄ ▄█▀▄▀  ████
█████▄▀   ▄█▄▀ ▀▀ ▄▀▄█ ▄█ ▀ ██▄█▄▄▄▀▄  ▀ ▀▄▄▄█▀▄ ▀▄▄▀█ █▄▀ ▀█ █ ▄▄  ▄ ▀█▄████
████ ▀▄▄█▄▄▄▄▀▄   ▀ ▄ ▄ █▀▀ ▀▄▀▀▄▀▀ ██▀▄█▄▀  ▀ ▀▀▀ ▀█ ▄█▀██▀██▀  ▄▀▄ ▀▀ ▀████
████▀█████▄▄█ ▄▄▀██▀▀█▀  █▄█▀▄█▀██▀▄ ▀▀ ▄ ▄ █▀▀▀████ ▀▀ ▀▀ █ ██▄▀▄██▀█ ▄▀████
████▄ ▀▄▄█▄█▀ ▀▀▀█▀    █ ███  ▀▀▄▄▀▀▀█▄▀  █▀ ▀▀▀█ ▀▀▀ ▄▄▄▄  ▄█▀  ▄▄▀▄▄ ▄▀████
████ ▀▄▀▄▄▄ ▀█▀▀▄▄▀ █ █ █▀▄▄ ▀▀▄▀▀▄██▄▄ █ ▄  █▄ ▄    ▀█▀ █  ▀█ ▄▀▀█▄▀█ █ ████
████▀ █  █▄██▀██▄█▄████▄█▀▀█▄▀▄█ ▄ ▄▀▄█▄▀███ ▀██▀ ▄█▀▄▄ ▄▀███▀▀█ ██ █▀▄█▄████
████ █ ▀▀ ▄█ ▀ ██▄██   ▄ █▄█▀▀█ ▄█▀█ ▀ ▄ ▀  ▄▀▄▄█▄ ▀ ▀   ▀▄  █▄ █▀ ▄ ▄  ▀████
████ ▄▄  ▄▄▄ ▀█▄▄▄▀▀▄▀ █▀▀ ▄▄█  ▄▀▀  ▄▄▄ █▀█▄ █▄▀▀██▄█▄ ▄█▄▀ ▀ ▄ ▄▄▄ █▀▀█████
████  ▄  █▄█ ▀▀▄▄▀▄█ █ ▄▄  █▄ ▄ ▄▀ ▄ █▄█ ▄ ▄▄▄▄▀▄█▀▀▄ █▀▀▀▄ █ ▀▄ █▄█ █▀▄ ████
█████▄▄  ▄▄▄▄█▄▀▄█▄▄▀  █ ▄█ ▄▄▄█▄  ▀▄ ▄ ▄▄  ▄▀▀█▄▄▀▄ █▀█▀▀█ █ ▀▄▄▄ ▄▄ █▀▄████
████  ▀▀█▄▄▀██ ▀▄▄█  █▀ ▀  ▀ █ ▄██▄▄▀█▄  ▀▀▀█ ▄▄ ▀▄▄  ▀ ▀ ▄▀█▄  ██▀█ ▀▀▄▄████
████▄▄▀ █ ▄▀  █▀██ █▀█▀▄▄ ▀▄▄▀▄▀▀ ▀▄█ ▄▀█▄▀▄█▄▄█ ▄     █▄▄▀▀  ▀ █▄ ██  ▀▄████
██████  ▄▀▄▄  █  ▄ █▄█ ▄▄█  █▄▀▄▄▀▄███▄▀ ▄█▀▄█ ▀█ ▄  ▀ ▄▀▄▀▄ █  ▀▀▀▄▄▀▄█ ████
████ ▄ ▀█ ▄▀█ ▀▀██  ▀ ██▀▀▀▄▄▀ ▄██▀▄██ ▀ █▀██▀▀█ ██▄  ▄ ▄▄▀▀█ █▀ ▄██▄ ▄▀▄████
████▄▀█▀▄ ▄▄█▀█▀▄▄▀▄▄▀█▀▄ ▀▄█▀▄ ▀▀▀ ██▄▀  ▄  █▄▄█ ██▀▄ ▄▀▀▄▀▄▄▀ ▀▀▄█ █ █▄████
████▀▀ ▄ █▄  █ ▄██▀█ █▀▄▄▄█▄▄▄ █▄  ▀█ ▄▄█▄   ▀▀▄ ▄ ▄▀ ▄ ▀▄▄▄▀██▄▀█ ▄██▀█ ████
████ █▀███▄ ▀▀▄  ▄▄ █ ▄ ▄█▀█ ▄██ ▄██▀ █▀▄▀█▄█▀▄  ▀▄▀▄▀▀▄▀ ▄▀█▀▀█▄▀ ▀▀▄▀▄▀████
████ ▄██▄█▄  ▀▀█ ▀▄█  ▄▄▄█ ▀  ▀███ █▀▀ ▀▄ ▄▄▀█▄ ▄ ██▄▄ ▄▄ ▀▄▀██  ▀▄  ▄██▄████
████▄█ █▄█▄    ▀▀ ███▄▄█ █▀▀ ▀█  ▄ ▀▀█ █▄█ ▄▄▄▄▄▀▀ ▄  ▄█▄█  ▄ █▄ ▀ ██ ▄██████
████▄▀ ▀▄ ▄█ █▀▄ █▄ ▄▀ ███▀▄  ▀▀▄█ ▄█ █ ▄█▀▄▄▄█ █ ██ ▀█▀█ ▄▀▀▀  ▄▀██▄ ▄▀▀████
████ █▄█▄▄▄ ▀ ▀█ ▄█ ▄ ▄▀███▀  ████ █ █ ██▄██▀▀▄█▀▄▀  ▀▄ ▀▄ ▀█▀█ ▄█▀█▀ █▀█████
████▄██▄▄█▄▄ ▀█▄██  ▀▀▀▀▄█ ▀ █▀▀▄▄▀▄ ▄▄▄ ▀ ██▀▄█▀▄██▀ ▀██ ▀██ █  ▄▄▄ █  ▄████
████ ▄▄▄▄▄ ██ █▀▄▄▄▀▀▀  ▀▀▀▀ █▄█▀▀█▄ █▄█ ▀█ ▀▄  █▄ ▀▀▀▀▀▄█▄  █ █ █▄█ ▀▀ ▄████
████ █   █ █   ▄▄ █▀█▄ ▄▄▀ █▄▄█ █▄ ▀▄▄▄ ▄▀ ▀█   ▀▄█▀█ ▄ ██▀█ ▀▀▄     █▀▄▄████
████ █▄▄▄█ █▀▄██▀▀▀▄  ▄▀ █▀  █  ▀  ▄▀▀   █▄▄█▄▄▄ █ ▄▀█ ▀█▀▄▀▀▀███   █    ████
████▄▄▄▄▄▄▄█▄▄█▄█▄██▄█████▄██▄██▄▄▄██▄██▄██▄▄█▄███▄▄█▄▄▄▄█▄███▄█▄██▄▄████████
█████████████████████████████████████████████████████████████████████████████
█████████████████████████████████████████████████████████████████████████████

As you can see it prints QR Code in UTF8 encoding. Next open your iOS camera and scan it. You’ll get your content directly on your phone, without transferring to any network interfaces.

Let’s say you want to generate QR Code for a very large string. QR Code is limited so you can split the images. For example, you need to transfer your gpg public key and generate multiple QR Codes with it.

$ gpg --export -a johndoe@example.com | qrencode -Sv40 -o gpg.png

The command above will create multiple gpg-*.png files. You can scan all of them to get your data.

So that’s it for today. Hope this tutorial useful for you. Thanks for reading.

Cover Photo by Mitya Ivanov on Unsplash.