You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/heredocs/about.md
+49-11Lines changed: 49 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -152,27 +152,34 @@ Two plus two is calculated by $((2 + 2))
152
152
153
153
### Stripping Leading Tabs
154
154
155
-
If you use `<<-` (with a trailing hyphen) instead of `<<`, Bash will strip leading _tab characters_ from each line of the heredoc.
155
+
If you use `<<-` (with a trailing hyphen) instead of `<<`, Bash will strip any leading _tab characters_ from each line of the heredoc.
156
156
This is useful for indenting the heredoc content within your script without affecting the output.
157
157
158
158
```bash
159
159
# Note, the leading whitespace is tab characters only, not spaces!
160
-
#and the ending delimiter can have leading tabs as well
160
+
#The ending delimiter can have leading tabs as well.
161
161
cat <<- END
162
-
This line has a leading tab.
163
-
This line also has a leading tab.
164
-
END
162
+
This line has 1 leading tab.
163
+
This line has a leading tab and some spaces.
164
+
This line 2 leading tabs.
165
+
END
165
166
```
166
167
167
-
The output is printed without the leading tabs:
168
+
The output is printed with all the leading tabs removed:
168
169
169
170
```plaintext
170
-
This line has a leading tab.
171
-
This line also has a leading tab.
171
+
This line has 1 leading tab.
172
+
This line has a leading tab and some spaces.
173
+
This line 2 leading tabs.
172
174
```
173
175
176
+
~~~~exercism/caution
174
177
The author doesn't recommend this usage.
175
-
While it can improve the readability of the script, it's too easy to accidentally replace the tab characters with spaces and it's too hard to spot the difference.
178
+
While it can improve the readability of the script,
179
+
180
+
1. it's too easy to accidentally replace the tab characters with spaces (your editor may do this automatically), and
181
+
1. it's too hard to spot the difference between spaces and tabs.
182
+
~~~~
176
183
177
184
## When to Use Here Documents
178
185
@@ -182,6 +189,37 @@ While it can improve the readability of the script, it's too easy to accidentall
182
189
* Scripting interactions: simulating user input for interactive programs.
183
190
* Avoiding external files: when you want to avoid creating temporary files.
184
191
192
+
A typical usage might be to provide some help text:
0 commit comments