How to use the command printf (with examples)
The printf
command is used to format and print text. It allows users to control the output format by specifying placeholders and formatting options. This command is commonly used for printing messages, formatting numbers, and composing text using variables.
Use case 1: Print a text message
Code:
printf "%s\n" "Hello world"
Motivation: This use case is useful when you want to print a simple text message, such as a greeting or an informational message.
Explanation:
- The
%s
is a placeholder for a string. - The
\n
is an escape sequence that represents a newline character.
Example output:
Hello world
Use case 2: Print an integer in bold blue
Code:
printf "\e[1;34m%.3d\e[0m\n" 42
Motivation: This use case can be used to highlight important numbers or to add emphasis to specific parts of the output.
Explanation:
- The
\e[1;34m
sequence sets the text color to bold blue. - The
%.3d
specifies that the argument is an integer with a minimum width of 3 digits. - The
\e[0m
sequence resets the text color to the default.
Example output:
042
Use case 3: Print a float number with the Unicode Euro sign
Code:
printf "\u20AC %.2f\n" 123.4
Motivation: This use case is useful when you need to print numbers with specific symbols or currency signs, such as displaying prices or monetary values.
Explanation:
- The
\u20AC
is an escape sequence that represents the Unicode Euro sign. - The
%.2f
specifies that the argument is a float number with a precision of 2 digits after the decimal point.
Example output:
€ 123.40
Use case 4: Print a text message composed with environment variables
Code:
printf "var1: %s\tvar2: %s\n" "$VAR1" "$VAR2"
Motivation: This use case is useful when you want to print a text message that includes the values of environment variables, allowing you to provide dynamic information.
Explanation:
- The
%s
is a placeholder for a string. - The
\t
is an escape sequence that represents a tab character. "$VAR1"
and"$VAR2"
are the values of theVAR1
andVAR2
environment variables, respectively.
Example output (assuming VAR1
is set to “foo” and VAR2
is set to “bar”):
var1: foo var2: bar
Use case 5: Store a formatted message in a variable (does not work on zsh)
Code:
printf -v myvar "This is %s = %d\n" "a year" 2016
Motivation: This use case allows you to store a formatted message in a variable for later use, enabling you to dynamically create and manipulate texts within your script.
Explanation:
- The
-v
option tellsprintf
to store the formatted message in a variable. - The
%s
is a placeholder for a string. - The
%d
is a placeholder for an integer.
Example output:
The variable `myvar` will contain the string "This is a year = 2016\n"
Conclusion
The printf
command is a versatile tool for formatting and printing text in a controlled manner. It offers a wide range of options and placeholders to cater to various printing requirements. By using printf
, users can easily customize their output, include variables, and create visually appealing messages.