-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf_tips_and_tricks.txt
133 lines (95 loc) · 6.4 KB
/
printf_tips_and_tricks.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Printf In Shell Scripting
Syntax And Basic Usage
The printf command in bash follows a simple syntax. It starts with the command itself, followed by a format string that specifies the desired output format. Here’s an example of the basic syntax:
1
printf "format_string" [arguments...]
The format string is enclosed in double quotes and can contain plain text and format specifiers. Format specifiers start with a percent sign (%) and are followed by a character that represents the data type to be formatted. For example, %s is used for strings, %d for integers, %f for floating-point numbers, and so on.
To print values using printf, you provide the corresponding argument/s after the format string. These arguments are substituted for the format specifiers in the order they appear.
Here’s a simple example that demonstrates the basic usage of printf:
1
2
3
name="John"
age=25
printf "Name: %s, Age: %d\n" "$name" $age
In this example, we have a format string “Name: %s, Age: %d\n” that contains two format specifiers: %s for the name (a string) and %d for the age (an integer). The corresponding values are provided as arguments after the format string (“$name” and $age). The \n at the end adds a newline character for better readability.
When you run the above code, it will output:
1
Name: John, Age: 25
Formatting Options
One of the key features of printf is its ability to format the output in a specific way. The format specifiers in the format string allow you to control the appearance of the printed values. Let’s explore some commonly used formatting options in printf:
Field Width: You can specify the minimum width of a field using a number before the format specifier. For example, %5s will allocate a minimum of 5 characters for a string. If the string is shorter than 5 characters, it will be padded with spaces to meet the specified width.
Precision Modifier: For floating-point numbers, you can specify the number of decimal places to display using a dot (.) followed by a number. For example, %.2f will display the number with two decimal places. If the number has fewer decimal places, it will be padded with zeros.
Left Alignment: By default, values are right-aligned within their field width. You can use a minus sign (-) before the field width to left-align. For example, %-5s will left-align a string within a 5-character field.
Zero Padding: You can use a zero (0) before the field width to pad numeric values with leading zeros instead of spaces. For example, %05d will pad an integer with leading zeros to meet a 5-character width.
Sign Display: You can use a plus sign (+) or a space before the format specifier to control the display of the sign for numeric values. The plus sign displays the sign for both positive and negative numbers, while the space displays the sign only for negative numbers.
Escape Sequences
Escape sequences in printf allow you to print special characters or control the formatting of the output. These sequences start with a backslash followed by a specific character or combination of characters. Here are some commonly used escape sequences in printf:
\n: Prints a newline character, causing the output to move to the next line.
\t: Prints a tab character, creating horizontal spacing in the output.
\\: Prints a backslash character.
\”: Prints a double quotation character.
\b: Prints a backspace character, which can be used to erase the previous character.
\r: Prints a carriage return character, which moves the cursor to the beginning of the line.
Here’s an example that demonstrates the use of escape sequences in print:
1
printf "Hello,\n\t\"World!\"\n"
When you run the above code, it will output:
1
2
Hello,
"World!"
In this example, the \n escape sequence is used to print a newline character, and the \t escape sequence is used to print a tab character. The \” escape sequence is used to print double quotes.
Escape sequences can be combined with format specifiers and formatting options to produce more complex output. For example, you can use \n to add line breaks between different pieces of formatted text.
Practical Examples
In this section, we will provide practical examples to demonstrate how printf can be used effectively in real-world scenarios. These examples will showcase the versatility and usefulness of printf in various scripting tasks. Let’s dive in:
Generating Formatted Reports
Printf is excellent for generating formatted reports. You can use it to align columns, format numbers, and create a visually appealing layout. For example:
1
2
3
printf "%-15s %-10s %10s\n" "Name" "Age" "Salary"
printf "%-15s %-10s %10.2f\n" "John Doe" 30 5000.50
printf "%-15s %-10s %10.2f\n" "Jane Smith" 25 6000.75
This will output a neatly formatted report with columns for Name, Age, and Salary.
Creating Progress Indicators
Printf can be used to create custom progress indicators in your scripts. By updating the output dynamically, you can show the progress of a task. For example:
1
2
3
4
for ((i=1; i<=10; i++)); do
printf "Progress: [%-${i}s] %d%%\r" "#" $((i * 10))
sleep 1
done
This will display a progress bar that updates every second, showing the completion percentage.
Formatting Table Data
Printf can help you generate a formatted table in your bash script. By using field widths and alignment options, you can create well-organized tables. For example:
1
2
3
4
printf "%-15s %-10s %10s\n" "Name" "Age" "City"
printf '%s\n' '--------------------------------------'
printf "%-15s %-10s %10s\n" "John Doe" 30 "New York"
printf "%-15s %-10s %10s\n" "Jane Smith" 25 "London"
This will produce a table with columns for Name, Age, and City, along with a separator line.
Conversion specifications (e.g. to format string or format number)
%s - to treat as a string
%c - to treat as a single character
%f - to treat as a floating number
%d - to treat as a signed integer
%u - to treat as an unsigned integer
%% - to print the percentage symbol
%X - to print with X character wide widths specified
%.X - to print with a precision modifier of X characters
%- - to print left-justified. note: by default it is right-justified.
format specifiers: string(s) that may contain one or more of:
Normal characters
Backslash-escaped characters
\b - backspace character
\n - newline character
\t - horizontal tab space
\v - vertical tab space
\” - quotation character
\\ - backslash character