|
|
|
The |
|
|
Basic printf syntax: printf FORMAT [ARGUMENTS…] |
|
|
Unlike echo, printf doesn’t add a newline automatically. Use \n to add newlines. |
|
|
Format specifiers: |
|
|
%s - String |
|
|
%d - Decimal integer |
|
|
%f - Floating point |
|
|
%e - Scientific notation |
|
|
%x - Hexadecimal |
|
|
%o - Octal |
|
|
%c - Single character |
|
|
%% - Literal percent sign |
|
|
Width and precision: |
|
|
Zero-padding: |
|
|
Multiple arguments: |
|
|
Arguments cycle through format: |
|
|
Escape sequences: |
|
|
Hex and octal escape codes: |
|
|
Store printf output in variable: |
|
|
Bash
Bash can store directly with -v flag: |
|
|
Practical examples: |
|
|
Create a table: |
|
|
Format currency: |
|
|
Leading zeros for file numbering: |
|
|
Progress percentage: |
|
|
Color output (ANSI escape codes): |
|
|
Format date components: |
|
|
Dynamic width: |
|
|
Print to stderr: |
|
|
Repeat character N times: |
|
|
Bash
Bash-specific: printf with arrays |
|
|
Common patterns: |
|