Writing custom scripts for Polybar


Polybar is perhaps the best and most beautiful statusbars for those who prefer a minimalist desktop and I have been using it along with i3 for the last three years. While it comes with enough built-in modules to satisfy most use cases, one of its strengths is that it is "highly customizable ... without the need of having a black belt in shell scripting".

My polybar

The script module is the easiest way to get some custom output on the bar. So lets quickly jump in with a simple example.

Polybar uses a configuration file, usually at ~/.config/polybar/config. Modules to be shown in the bar are added to modules-left, modules-right and modules-center. So lets go ahead and add a custom module on the right like this.

modules-right = hello-world

Now we need to define the module. We do that like this -

type = custom/script
exec = ~/polybar-scripts/hello-world.sh
interval = 5

Custom scripts have more functionality and options, but this is the simplest usage. Identify as a custom script, define the command / script to be executed and the interval at which it should be executed. The string returned by the command / script will be displayed on the bar. So our hello-world.sh script will look like this -

echo "Hello World"

That's it. Chmod the file to make it executable and "Hello World" should show up on the bar right away. Of course, this by itself is not useful, but it is an illustration of how easy it is to write custom scripts to display information on the bar. In a future post, I will walk through some of the simple scripts that I have written for my use.