/ factorpad.com / tech / linux-essentials / printf-command.html
An ad-free and cookie-free website.
Intermediate
Learn to format output at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux printf Command Summary with Examples (4:09)
Our ninety-second word, or command to memorize is
printf
from our category
Workflow.
printf
allows you to create formatted
output.
-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.
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.
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.
Now, watch as I type printf
format
followed by two arguments.
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.
You can see how that changes the output there.
Then since financial data has tabular form, we may format it like so.
So let's add printf
to our script.
And we've been here a few times.
(Below is the screen from within nano
.)
(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.
And I'll give you a few seconds to look at this. (The section below is after the pause and clear screen.)
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
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.
tee
command, hit Back.test
command, click Next./ factorpad.com / tech / linux-essentials / printf-command.html
A newly-updated free resource. Connect and refer a friend today.