Use Cases and Examples
Bash [.inline-code]printf[.inline-code] prints text on a terminal with multiple formatting options. It’s probably most useful to print in a shell script, but can be used on the command line as well.
In cases where we want more control over how content is displayed, we can leverage the additional formatting specifiers and arguments to fit our needs.
[#printf-syntax]printf syntax[#printf-syntax]
[.inline-code]printf [-v var] [format specifiers] [arguments][.inline-code]
var: optional and if used, the output will not be printed but will be assigned to the variable. This is not to be confused with printing variables which you can do by prefixing the variable with [.inline-code]$[.inline-code].
format specifiers: string(s) that may contain one or more of:
- Normal characters
- Backslash-escaped characters
- [.inline-code]\b[.inline-code] - backspace character
- [.inline-code]\n[.inline-code] - newline character
- [.inline-code]\t[.inline-code] - horizontal tab space
- [.inline-code]\v[.inline-code] - vertical tab space
- [.inline-code]\”[.inline-code] - quotation character
- [.inline-code]\\[.inline-code] - backslash character
Conversion specifications (e.g. to format string or format number)
- [.inline-code]%s[.inline-code] - to treat as a string
- [.inline-code]%c[.inline-code] - to treat as a single character
- [.inline-code]%f[.inline-code] - to treat as a floating number
- [.inline-code]%d[.inline-code] - to treat as a signed integer
- [.inline-code]%u[.inline-code] - to treat as an unsigned integer
- [.inline-code]%%[.inline-code] - to print the percentage symbol
- [.inline-code]%X[.inline-code] - to print with X character wide widths specified
- [.inline-code]%.X[.inline-code] - to print with a precision modifier of X characters
- [.inline-code]%- -[.inline-code] to print left-justified. note: by default it is right-justified.
arguments: can be any number of values and/or variables. In the case that there are more arguments than format specifiers, then the arguments will get reused.
Tip: If you are on zsh and you may be seeing a % at the end of each line. This is zsh's way of noting that the preceding command output was a partial line. You can disable it by updating the PROMPT_EOL_MARK configuration within your ~/.zshrc file.
[#readable-content][.inline-code]printf[.inline-code] makes it easy for engineers to output readable content[#readable-content]
Being able to use [.inline-code]printf[.inline-code] for all of its functionalities helps us as engineers understand our code easier and in a human-friendly way. For example, imagine you were trying to debug a script with the following variables.
In the example below, we don’t leverage the variable printing that [.inline-code]printf[.inline-code] has, so we end up hardcoding the currency amounts. Additionally, it is harder to read because it is all on one line, and all of the variables have different precisions.
Instead, if we utilize the capabilities of [.inline-code]printf[.inline-code] we can write legible statements that can be modified and built on top of. Below, we are printing the variables and formatting the output with the width specified and float precision.
[#echo-vs-printf][.inline-code]echo[.inline-code] vs [.inline-code]printf[.inline-code][#echo-vs-printf]
When writing scripts, [.inline-code]echo[.inline-code] is the simplest command to print to the standard output. By default, it adds a new line so that you don’t need to! You can print strings and variables and add options to escape backslashes.
While [.inline-code]echo[.inline-code] is straightforward to use, there are noted inconsistencies with its implementation, and is considered “non-portable”. Here is a stackexchange post that elaborates further. Instead, the [.inline-code]printf[.inline-code] command has become the replacement command and offers a wider range of capabilities in the way it formats and prints the arguments.
[#printf-vs-sprintf-vs-fprintf][.inline-code]printf[.inline-code] vs [.inline-code]sprintf[.inline-code] vs [.inline-code]fprintf[.inline-code][#printf-vs-sprintf-vs-fprintf]
In addition to the [.inline-code]printf[.inline-code] function, there are two other similar functions you may have seen.
[.inline-code]printf[.inline-code] is arguably the most common and used to print to the standard output stdout
[.inline-code]sprintf[.inline-code] does not print it to the standard output but rather stores it on a buffer
[.inline-code]fprintf[.inline-code] does not print to the standard output but rather prints to a file