HiveBrain v1.2.0
Get Started
← Back to all entries
snippetbashTip

printf — Format and print text. See also: `echo`. More information: <https://www.gnu.org/software/coreutils/m

Submitted by: @import:tldr-pages··
0
Viewed 0 times
commandformattextandcliprintseeprintf

Problem

How to use the printf command: Format and print text. See also: echo. More information: <https://www.gnu.org/software/coreutils/manual/html_node/printf-invocation.html>.

Solution

printf — Format and print text. See also: echo. More information: <https://www.gnu.org/software/coreutils/manual/html_node/printf-invocation.html>.

Print a text message:
printf "{{%s\n}}" "{{Hello world}}"


Print an integer in bold blue:
printf "{{\e[1;34m%.3d\e[0m\n}}" {{42}}


Print a float number with the Unicode Euro sign:
printf "{{\u20AC %.2f\n}}" {{123.4}}


Print a text message composed with environment variables:
printf "{{var1: %s\tvar2: %s\n}}" "{{$VAR1}}" "{{$VAR2}}"


Store a formatted message in a variable (does not work on Zsh):
printf -v {{myvar}} {{"This is %s = %d\n" "a year" 2016}}


Print a hexadecimal, octal, and scientific number:
printf "{{hex=%x octal=%o scientific=%e\n}}" 0x{{FF}} 0{{377}} {{100000}}

Code Snippets

Print a text message

printf "{{%s\n}}" "{{Hello world}}"

Print an integer in bold blue

printf "{{\e[1;34m%.3d\e[0m\n}}" {{42}}

Print a float number with the Unicode Euro sign

printf "{{\u20AC %.2f\n}}" {{123.4}}

Print a text message composed with environment variables

printf "{{var1: %s\tvar2: %s\n}}" "{{$VAR1}}" "{{$VAR2}}"

Store a formatted message in a variable (does not work on Zsh)

printf -v {{myvar}} {{"This is %s = %d\n" "a year" 2016}}

Context

tldr-pages: common/printf

Revisions (0)

No revisions yet.