If you've been using the Bash shell for a decent amount of time, you probably know how to print strings in the Linux terminal using the echo command. The printf command, however, makes printing text with specific formatting much easier.

Today we'll learn how to make use of the printf function in order to enhance our Bash scripting skills.

What Is the Bash printf Function?

As the name suggests, printf is a function that prints formatted strings of text. That means you can write a string structure (the format) and later fill it in with values (the arguments).

If you're familiar with the C/C++ programming languages, you might already know how printf works. Printf in the Bash shell is very similar, but it does have a few small differences.

Learn More: Best Sites to Get Started With C++ Programming

How to Use printf in the Bash Shell

The basic syntax for printf is:

        printf format [argument]
    

Printf will print the format string while implementing escapes (\) and directives (%) through the use of specified arguments. Note the output of the following command:

        $ printf "Hello, %s" world
Hello, world

Printf took the world argument and replaced the %s character in the specified string with it.

You may notice that after the output, Bash doesn't create a new line for you. Unlike the echo command, printf won't assume you want a new line after the output is printed. Thus, you'll need to use the new line character \n in each instance.

To print a string and then move to a new line, type printf "Hello, world\n".

        Hello, world
    

It's important to keep in mind that printf will attempt to reuse the format for all of your arguments. You can use this to your advantage, but if you're not careful, you can end up with results like this:

        $ printf "My name is %s " Jordan Gloor
My name is Jordan My name is Gloor

You should also be aware that if an argument is missing, printf will interpret any directives as 0 (for a number) and null (for a string).

        $ printf "Hello, %s."
Hello, .

Now that you know how Bash scripting with printf in Linux works, you're ready to start using it in your commands.

Linux Bash printf Examples

Printf features many format specifiers that you can use in your scripts. But today we'll only be covering some common ones for the function.

1. Format Output With a String

To format the output with strings of text, use the %s directive.

        $ printf "%s is one of the largest online %s." MUO "technology publications"
MUO is one of the largest online technology publications.

2. Format Output String With Decimals

To format a string with an integer, you can use the %d directive for a signed decimal.

        $ printf "MUO was founded in %d." 2007
MUO was founded in 2007.

Note that if you want to use an unsigned decimal in the output, you will have to use the %u directive instead.

Related: What Does "Bash" Mean in Linux?

3. Output Math Functions Using printf

Formatting math functions with the printf command is easy. Just place your expression inside of double parentheses like you normally do in Bash, and specify the expression in the arguments list.

        $ printf "1 + 1 is %d" $((1+1))
1 + 1 is 2

4. Format Hexadecimal Numbers

If you need to format a hexadecimal number, use %x for lowercase and %X for uppercase.

        $ printf %X 1000
3E8C

To print the current date and time, you can combine printf with the date command and pass the following directives.

        $ printf "%(%m-%d-%Y %H:%M:%S)T" $(date +%s)
03-26-2021 15:27:57

You can see the format specifiers for month, day, year, hour, minute, and second in the aforementioned output.

6. Format Strings With Unicode Characters

To print Unicode characters with printf, use the \u escape for 16-bit Unicode and \U for 32-bit Unicode.

For example, you can print the copyright symbol with the following command:

        $ printf "\u00A9"
©

7. Add Spacing to the Output

You can format your strings with spacing by defining, before the directive specifier, a minimal number of characters printed. Unused characters will be filled in with spaces.

For example, typing printf "%s:%5d\n" "Value 1" 25 "Value 2" 120 will output:

        Value 1:   25
Value 2: 120

The %5d format specifier in the output denotes that the formatted string will contain a minimum of five characters. Since 25 has only two digits, spaces are added to the remaining characters.

If you pass a negative number as the width, the directive will be left-justified instead of right-justified.

        $ printf "%-10s: %d " "Circles" 25 "Boxes" 120
Circles : 25 Boxes : 120

If you want to make the width variable, you can pass an asterisk instead of a number, and printf will use the next number it finds in the argument list.

        $ printf "%*s: %d " -10 "Circles" 25 -10 "Boxes" 120
Circles : 25 Boxes : 120

Related: Everything You Need to Know About Bash For Loops in Linux

Learning the Linux printf Command

With the printf command in your Bash scripting utility belt, you're ready to start printing complex and varying strings in your terminal. As you're developing your Bash scripts, it is important to make sure that you're working in the most productive environment possible. This will not only improve your command-line skills but will also enhance your workflow in general.