From the course: Learning Bash Scripting

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Formatting output with printf

Formatting output with printf - Bash Tutorial

From the course: Learning Bash Scripting

Formatting output with printf

- [Narrator] As we've seen with echo composing strings that include variables or other data can get a little bit tedious, and so I want to show you another way to do it. The printf builtin gives us the ability to use placeholders when we compose strings and format values in ways that can make our script and our output look cleaner and more organized. Let's compare getting the same output result with both echo and printf. Both of these statements print "The results are: four and three." Using printf, we'll write the literal parts of our string, the parts that don't change, and instead of putting Bash code mixed in with the string we use a placeholder wherever a dynamic value, one that will be the result of something our script runs will go and then at the end of the line we'll put whatever code we'll provide those values in the same sequence we used for the placeholders. If we provide more values than there are placeholders…

Contents