FactorPad
Build a Better Process

Linux printf Command Summary with Examples

Linux printf allows you to create formatted output.
  1. Purpose - Learn what printf is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with printf.
  4. Script - Add printf to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux printf, 92 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / printf-command.html


An ad-free and cookie-free website.


Examples of the Linux printf Command

Intermediate

Learn to format output at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux printf Command Summary with Examples (4:09)

Video Script

The Command and Why You Need It

Our ninety-second word, or command to memorize is printf from our category Workflow.

printf allows you to create formatted output.

Common Linux printf Options
-options description
--help Print help screen
-v Create a variable instead of send to standard output

Recall from videos (tutorials) #87 to #91, we're building a script to demonstrate workflow, now we'll cover the printf command and add it to a script.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The printf command has 3 options and the argument is a typically a string of text that is preceded by a format specification that you'd like the argument to look like.

Like most commands, help is available with double-dash --help, however printf is often built-in to the shell, so if that's the case on your end, read the bash man page, and -v on some versions assigns the argument to a variable.

So why is printf an important command? Well, it helps to make output pleasing to the eye. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's start with type -a on printf and see how many versions you have.

$ type -a printf printf is a shell builtin printf is /usr/bin/printf

This is just to get ahead of anything, in case there's any confusion out there. Mine has two as you can see and bash is the default unless we type the specific path on the second one. And then you can also look at --help as an example of the difference between the two.

$ printf --help -bash: printf: --: invalid option printf: usage: printf [-v var] format [arguments] $ /usr/bin/printf --help Usage: /usr/bin/printf FORMAT [ARGUMENT]... or: /usr/bin/printf OPTION Print ARGUMENT(s) according to FORMAT, or execute according to OPTION: --help display this help and exit --version output version information and exit FORMAT controls the output as in C printf. Interpreted sequences are: \" double quote \\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \NNN byte with octal value NNN (1 to 3 digits) \xHH byte with hexadecimal value HH (1 to 2 digits) \uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits) \UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits) %% a single % %b ARGUMENT as a string with '\' escapes interpreted, except that octal escapes are of the form \0 or \0NNN and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs converted to proper type first. Variable widths are handled. NOTE: your shell may have its own version of printf, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Full documentation at: <http://www.gnu.org/software/coreutils/printf> or available locally via: info '(coreutils) printf invocation' $ clear

Now, watch as I type printf format followed by two arguments.

$ printf "I like %s eggs and %s.\n" green ham I like green eggs and ham.

That gives you an idea of what it can do.

And then next, let's look at formatting, %s for strings, which you just saw, %d is decimal and %f is for floating point, using the same inputs 10, 10 and 10.

$ printf "%s %d %f" 10 10 10 10 10 10.000000

You can see how that changes the output there.

Then since financial data has tabular form, we may format it like so.

$ printf "%s\t%s\t%s\n" Date Market TBill Date Market TBill

So let's add printf to our script. And we've been here a few times.

$ nano ~/bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date # From video 88, we measure the time to run the script sleep 2s # From video 89, we take input from the keyboard echo -e "What is your name? \c" read yourname echo "Thank you $yourname" # From video 90, report files and lines since last archive echo "Lines and files since your last archive:" find . -type f -cnewer "video73.tar" | xargs wc -l # From video 91, use tee to print files and show last five echo "You've been busy. See video91.txt for a list of files" echo "Here are your latest five files. Nice work!" ls *.txt | tee video91.txt | tail -5 # From video 92, use printf to start our next section sleep 4s clear printf "Ok, %s, what would you like to do next?\n" $yourname [ Read 35 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

(Here we added lines about video 92 and below.)

At this section, here's a pause, and then a clear screen and we finish with a question from the variable that we collected earlier.

Okay, please pause if you'd like to catch up on your end, and then we'll try it out.

(Hit Ctrl-x to leave nano and y to confirm saving.)

Okay, and last, explicitly run it like so, and here is our work in progress.

$ ~/bin/funscript The current date and time: Mon Nov 15 17:22:14 PST 2016 What is your name? Paul Thank you Paul Lines and files since your last archive: 0 ./video79.script 5 ./video85.txt 0 ./video75.txt 0 ./video76.txt 61 ./video85b.txt 66 total You've been busy. See video91.txt for a list of files Here are your last five files. Nice work! video75.txt video76.txt video85a.txt video85b.txt video91.txt _

And I'll give you a few seconds to look at this. (The section below is after the pause and clear screen.)

Ok, Paul, what would you like to do next? $ _

A Final Tip

Okay now you know how to use printf. And you know the syntax for commands, options and arguments.

One last tip about the printf command. So printf has so much to offer that many programming languages have similar functions built in.

Okay, thanks for visiting today. I hope this was a helpful introduction to the printf command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

A unique combination of ad-free tutorials in HTML and companion videos on YouTube. Join, follow and subscribe now so you don't lose this resource.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / printf-command.html


linux printf command
linux printf examples
printf format
printf bash
string format
bash printf
printf float
printf string
man printf
printf syntax
printf linux
shell printf
shell script
bash script
printf integer
printf example
printf command

A newly-updated free resource. Connect and refer a friend today.