how to format text in POSIX sh

apr 21 2022


outputting formatted text in pure POSIX sh (or bash) is honestly a nightmare. but anyways, here's how to do it.

OPTION 1: the printf way

using printf is probably the "best" way to do it, but also the hardest. here's an example of what it looks like:

#!/bin/sh
printf "\033[1m%s\033[m\n" "foo"

that doesn't exactly make much sense to anyone looking at it. so what does it mean? let's break it down.

as you can see, it's a bit of a mess, and hard to parse for anyone who isn't deeply familiar with the syntax.

by the way, if you want to see all the escape sequences, check here

OPTION 2: yodel

i made a script to fix this problem, called yodel. here's an example of the syntax:

#!/bin/sh
yodel --bold "foo"

under the hood, it uses the exact same method as above, but it makes it much easier to tell what's going on by reading the code.