Linux Comm Printf
[ Linux Command Encyclopaedia](#)\n\n* * *\n\n## 1. printf Command Overview\n\nprintf is a powerful formatted output command in Linux/Unix systems, derived from the printf() function in C language. Compared to the echo command, printf provides more precise output control and formatting capabilities.\n\n### Main Features\n\n* **Precise formatting**: Can control output alignment, width, precision, etc.\n* **No automatic newline**: Does not add a newline character at the end of output by default (different from echo)\n* **Multi-language support**: Supports Unicode character output\n* **Variable insertion**: Can insert variable values in strings\n\n* * *\n\n## 2. Basic Syntax\n\nprintf format-string [arguments...]\n### Parameter Description\n\n* **format-string**: Format string containing ordinary characters and format specifiers\n* **arguments**: Parameter list corresponding to format specifiers\n\n* * *\n\n## 3. Format Specifier Details\n\nFormat specifiers start with `%`, with the basic form being:\n\n%[.precision]specifier\n### Common Format Specifiers\n\n| Specifier | Description | Example |\n| --- | --- | --- |\n| %s | String | printf "%s" "hello" |\n| %d | Decimal integer | printf "%d" 123 |\n| %f | Floating point number | printf "%f" 3.14 |\n| %x | Hexadecimal integer (lowercase) | printf "%x" 255 |\n| %X | Hexadecimal integer (uppercase) | printf "%X" 255 |\n| %o | Octal integer | printf "%o" 8 |\n| %c | Single character | printf "%c" 65 |\n\n### Modifier Options\n\n#### 1. Flags\n\n| Flag | Description | Example |\n| --- | --- | --- |\n| - | Left align | printf "%-10s" "hi" |\n| + | Show sign | printf "%+d" 123 |\n| 0 | Zero padding | printf "%05d" 12 |\n| space | Space before positive numbers | printf "% d" 123 |\n| # | Special format (e.g., 0x prefix) | printf "%#x" 255 |\n\n#### 2. Width\n\nSpecifies the minimum field width, padding when insufficient\n\n## Example\n\nprintf"%10sn""hello"# Right align, width 10\n\nprintf"%-10sn""hello"# Left align, width 10\n\n#### 3. Precision\n\nFor floating point numbers, specifies the number of decimal places; for strings, specifies the maximum number of characters\n\n## Example\n\nprintf"%.2fn"3.14159# Output 3.14\n\nprintf"%.5sn""abcdefg"# Output abcde\n\n* * *\n\n## 4. Practical Examples\n\n### Example 1: Basic Formatted Output\n\n## Example\n\nprintf"Name: %s, Age: %d, Height: %.2fn""Alice"25 1.68\n\nOutput:\n\nName: Alice, Age: 25, Height: 1.68\n### Example 2: Table Alignment Output\n\n## Example\n\nprintf"%-10s %-10s %-10sn""Name""Age""Score"\n\nprintf"%-10s %-10d %-10.2fn""Alice"25 89.5\n\nprintf"%-10s %-10d %-10.2fn""Bob"23 92.3\n\nOutput:\n\nName Age Score Alice 25 89.50 Bob 23 92.30 \n### Example 3: Special Character Handling\n\n## Example\n\nprintf"Temperature: %dΒ°Cn"25\n\nprintf"Path: %sn""/home/user"\n\nprintf"Alert: an"# Bell character\n\n### Example 4: Variable Insertion\n\n## Example\n\nname="John"\n\nage=30\n\nprintf"User: %s, %d years oldn""$name""$age"\n\n* * *\n\n## 5. Advanced Usage\n\n### 1. Formatted Date Output\n\n## Example\n\nprintf"Today is %(%Y-%m-%d)Tn"-1\n\n### 2. Color Output\n\n## Example\n\nprintf"e[31mRed Texte[0mn"\n\nprintf"e[32mGreen Texte[0mn"\n\n### 3. Dynamic Width Specification\n\n## Example\n\nwidth=20\n\nprintf"%*sn"$width"Right aligned"\n\n* * *\n\n## 6. Common Issues and Notes\n\n**Newline Issue**: printf does not add a newline character by default, you need to manually add `n`\n\n## Example\n\nprintf"No newline"\n\nprintf"Add newlinen"\n\n**Parameter Count Mismatch**: When there are fewer parameters than format specifiers, undefined values will be output\n\n## Example\n\nprintf"%s %sn""only_one"# The second %s will output empty\n\n**Quote Handling**: It is recommended to enclose the format string in double quotes, and variables should also be in double quotes\n\n## Example\n\nprintf"%sn""$variable"\n\n**Special Character Escaping**: Special characters need to be escaped with backslashes in the format string\n\n* * *\n\n## 7. printf vs
YouTip