How to generate QR code from Linux Terminal

  • Post last modified:September 29, 2021
  • Reading time:5 mins read

How often do we need to share a URL from the computer to another device or do you want to attach a QR code to an email? Instead of writing a long URL.

We have a very good chance of making a mistake when typing, personally, I get frustrated when typing a long URL so I use KDE Connect for all the content sharing and that is a different topic which we will discuss later.

Most of the Linux users are too affectionate with a terminal screen, and usually prefer to complete their tasks from the terminal itself rather than using GUI.

I also believe that the terminal is better than the GUI. For that reason, I’ve come up with a new topic called “Generate QR Code from a Linux Terminal”.

Prerequiste

You don’t need to install any application or utility from the repository or server to generate a QR code, just make sure that you have an active internet connection while doing the below steps, and the last curl should be available on your computer.

I hope you have the following things to complete the next steps.

Generate QR code from Linux Terminal

There are many tools to do this but I have selected “qrcode.show” to generate the QR code.

One important point is that you don’t need to install any application to do it, just need a curl and active internet which I believe is always available for one.

Some of the features, I’ll like to list down below before proceeding ahead:

  • They don’t like to store scanned details, you can rely them for privacy.
  • Amazingly Fast and simple API that works on both web and terminal.
  • It’s Supports GET and POST requests.
  • Supports Accept header to control the output format(png, xml, svg and other).

There are several features that will be rolling out in the coming days.

How to use qrcode.show to generate QR code

I’ll start with the basic usage and later will show the advanced options too. First, get familiar with the use of the command.

Simple & elegant

If you want to share some of your favorite URLs with your friend but it’s quite long, and don’t want to type? use the following command:

$ curl qrcode.show/INPUT

Of course, replace Input with valid text.

For example, I want to share with you a post that will help you find out who is logged into your system in your absence.

$ curl qrcode.show/https://trendoceans.com/how-to-check-who-has-logged-into-my-linux-system/
Scan me, please!

Behave like browser

It is similar to a web browser in which once submits button is clicked it makes a post request to a web server.

 $ curl qrcode.show -d INPUT
  • -d:- parameter or flag stands for data and awaiting for Input.
  • INPUT:- pass the content

I’ll use the above example once again to demonstrate the above code snippet:

$ curl qrcode.show -d https://trendoceans.com/how-to-check-who-has-logged-into-my-linux-system/
Post request example

Genrate QR code from text file

Nowadays it is quite common for QR codes to contain multiple details such as event name, venue details, contact details, and other information.

If you are also looking for the same, and want to create a QR code? Alright, first, let me create a dummy file with minimal details and pass the below command to generate a QR code from the terminal.

$ curl qrcode.show -d @imp_details
  • -d:- Accept input
  • @:- Read the data from respecitve path or file

Great, next month I’ll host an event. Thank you, friend.

Save QR code to your local computer

Of course, we can take a snip of QR code and share, instead, we might have something more convenient that saves the output for later use on our local computer.

One can alter the default (application/octet-stream) header response to various bitmap formats like SVG, PNG, JPEG.

Write code as usual but there is a twist you need to use -H (header) flag, pass the required bitmap format, and last mentioned the path to store QR code.

$ curl qrcode.show/INPUT -H "Accept: image/jpeg" --output ./dummy 

Run qrcode locally

Hey, folks every time you type curl and a long list of commands to get a single task done, that’s not quite acceptable for regular tasks.

Alternatively, we can create an alias and use it whenever we need but they have already provided a script that makes our work easier.

You can copy-paste the following code snippet and save it in ~ / .bashrc file and voila you’re app is ready to use without typing long code.

qrcode () {
          local input="$*"
          [ -z "$input" ] && local input="@/dev/stdin"
          curl -d "$input" https://qrcode.show
        }

        qrsvg () {
          local input="$*"
          [ -z "$input" ] && local input="@/dev/stdin"
          curl -d "${input}" https://qrcode.show -H "Accept: image/svg+xml"
        }

        qrserve () {
          local port=${1:-8080}
          local dir=${2:-.}
          local ip="$(ifconfig | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | fzf --prompt IP:)" \
            && echo http://$ip:$port | qrcode \
            && python -m http.server $port -b $ip -d $dir
        }

Check if the script is working by passing the code below:

$ qrcode "Share this article around your circle!"

It’s doesn’t end here if you know how to write a bash script then you can customize the script as per your preference and use it.

Wrapping-up

That’s all to “generate QR code from Linux Terminal”.

There are several options that I have not covered in this short guide, For more parameters visit the official webpage or refer to Github.

Read this:- 5 Funny Linux commands, Literally, we can do?

If you know this is an early stage of application development, it would be a great help if you could collaborate to improve or add functionality.

Please let us know how this article helps you to accomplish your task.

Like what you read? Please share it with others.
  • 1
    Share

Leave a Reply