Learn to format output at the Linux command line.
Linux printf command summary with examples (4:09)
Videos can also be accessed from the YouTube Playlist.
Our ninety-second word, or command to memorize is
printf from our category
printf allows you to create formatted
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).
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
printf is often built-in to the shell,
so if that's the case on your end, read the
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
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
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
(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.
Ctrl-x to leave
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
And you know the syntax for commands, options and arguments.
One last tip about the
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
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
Click here to join our growing YouTube Channel.
teecommand, hit Back.
testcommand, click Next.