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