Bash printf syntax examples

This tutorial will explore the bash printf command with syntax examples on Linux systems. When writing bash scripts, most of us by default use the echo command as a means to print to standard output stream.

echo is easy to use and mostly it fits our needs without any problem. However, with simplicity very often comes limitation. This is also the case with echo command. Formatting an echo command output can be a nightmare and very often impossible task to do.

The solution to this can be a good old friend of all C/C++ coders, the printf tool. printf can be just as easily implemented into a bash script as it is used with C/C++ programs. This article describes some basics of printf along with practical examples.

In this tutorial you will learn:

  • How to use printf command on Linux
  • printf syntax examples for Bash scripting
Bash printf syntax examples
Bash printf syntax examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

printf Syntax




printf accepts a FORMAT string and arguments in the following general form:

$ printf 
  1. prinft can have format specifiers, escape sequences or ordinary characters. When it comes to arguments, it is usually text we would like to print to standard output stream. Let’s start with something simple from a bash shell command line:
    $ printf "hello printf"
    hello printf$
    

    By default, no new line is printed in the printf output
    By default, no new line is printed in the printf output
  2. At this point, we have supplied an argument, “hello”. Note the different behaviour in comparison to echo command. No new line had been printed out as it it in case of when using default setting of echo command. To print a new line we need to supply printf with format string with escape sequence \n ( new line ):
    $ printf "%s\n" "hello printf"
    hello printf
    
  3. The format string is applied to each argument:
    $ printf "%s\n" "hello printf" "in" "bash script"
    hello printf
    in
    bash script
    

printf Format Specifiers

As you could seen in the previous simple examples, we have used %s as a format specifier. The most commonly used printf specifiers are %s, %b, %d, %x and %f. The specifiers are replaced by a corresponding argument. See the following example:

$ printf "%s\t%s\n" "1" "2 3" "4" "5"
1       2 3
4       5

In the example above, we have supplied two specifiers %s to print TAB \t and NEWLINE \n to be used as a part of thr printf format string to print along with each argument. First \t is applied to argument “1” and \n is applied to argument “2 3”. If there are more arguments than specifiers, the format string is reused until all arguments have been depleted. Specifier %s means to print all argument in literal form.

printf Examples

As we have now covered the bare basics, let’s see some more printf examples.

  1. Instead of the %s specifiers, we can use the %b specifier, which is essentially the same but it allows us to interpret escape sequences with an argument:


    $ printf "%s\n" "1" "2" "\n3"
    1
    2
    \n3
    $ printf "%b\n" "1" "2" "\n3"
    1
    2
    
    3
    $
    
  2. When it comes to printing integers, we can use the %d specifier:
    $ printf "%d\n" 255 0xff 0377 3.5
    255
    255
    255
    bash: printf: 3.5: invalid number
    3
    

    As you can see, %d specifiers refuse to print anything other than integers.

  3. To print floating point numbers, the %f specifier is our friend:
    $ printf "%f\n" 255 0xff 0377 3.5
    255.000000
    255.000000
    377.000000
    3.500000
    
  4. The default behaviour of the %f printf specifier is to print floating point numbers with 6 decimal places. To limit the decimal places to 1, we can specify a precision in the following manner:
    $ printf "%.1f\n" 255 0xff 0377 3.5
    255.0
    255.0
    377.0
    3.5
    
  5. Formatting to three places with preceding zeros:
    $ for i in $( seq 1 10 ); do printf "%03d\t" "$i"; done
    001     002     003     004     005     006     007     008     009     010
    



  6. Here’s a simple table. What it does is format names to 7 places and max 7 characters, and format floating point number to 9 places with 2 decimals. More complicated sample script using printf formatting to create a table with multiple items. Save as a script make executable and run:
    #/bin/bash
    divider===============================
    divider=$divider$divider
    
    header="\n %-10s %8s %10s %11s\n"
    format=" %-10s %08d %10s %11.2f\n"
    
    width=43
    
    printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"
    
    printf "%$width.${width}s\n" "$divider"
    
    printf "$format" \
    Triangle 13  red 20 \
    Oval 204449 "dark blue" 65.656 \
    Square 3145 orange .7
    

    Output:

    $ ./table 
    
     ITEM NAME   ITEM ID      COLOR       PRICE
    ===========================================
     Triangle   00000013        red       20.00
     Oval       00204449  dark blue       65.66
     Square     00003145     orange        0.70
    

Closing Thoughts

In this tutorial, we saw how to use the printf Linux command through syntax examples. The printf command is a nice alternative to echo, with lots of options to expand on its functionality, which makes it ideal for Bash scripting. C and C++ programmers should find the printf command very familiar, as it works the same in Bash.



Comments and Discussions
Linux Forum