2015-12-28 11:32:49 -02:00
|
|
|
# printf
|
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
> Format and print text.
|
2021-04-01 17:54:26 +02:00
|
|
|
> More information: <https://www.gnu.org/software/coreutils/printf>.
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Print a text message:
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2020-10-04 19:33:38 +02:00
|
|
|
`printf "{{%s\n}}" "{{Hello world}}"`
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Print an integer in bold blue:
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2020-10-04 19:33:38 +02:00
|
|
|
`printf "{{\e[1;34m%.3d\e[0m\n}}" {{42}}`
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2021-05-20 16:13:41 -04:00
|
|
|
- Print a float number with the Unicode Euro sign:
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2020-10-04 19:33:38 +02:00
|
|
|
`printf "{{\u20AC %.2f\n}}" {{123.4}}`
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Print a text message composed with environment variables:
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2020-10-04 19:33:38 +02:00
|
|
|
`printf "{{var1: %s\tvar2: %s\n}}" "{{$VAR1}}" "{{$VAR2}}"`
|
2015-12-28 11:32:49 -02:00
|
|
|
|
2016-01-07 18:31:27 +01:00
|
|
|
- Store a formatted message in a variable (does not work on zsh):
|
2015-12-28 11:32:49 -02:00
|
|
|
|
|
|
|
`printf -v {{myvar}} {{"This is %s = %d\n" "a year" 2016}}`
|
2024-03-04 00:47:08 +11:00
|
|
|
|
|
|
|
- Print a hexadecimal, octal and scientific number:
|
|
|
|
|
|
|
|
`printf "{{hex=%x octal=%o scientific=%e}}" 0x{{FF}} 0{{377}} {{100000}}`
|