-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat.html
167 lines (131 loc) · 4.07 KB
/
cat.html
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Useful use of cat</title>
<meta name="description" content="It is fine to begin your shell commands with cat">
<style>
body {
margin: 4em;
line-height: 1.4;
max-width: 34rem;
}
@media (width < 40rem) {
body {
margin-block: 2em;
margin-inline: 1em;
}
}
body {
margin-block-end: 10rem;
}
@media (prefers-color-scheme: dark) {
body {
background-color: hsl(240, 44%, 8%);
color: rgb(183, 247, 162);
}
h2 {
color: rgb(156, 228, 33);
}
code, kbd {
color: rgb(156, 228, 33);
background-color: black;
padding: 0.6ch;
border: 1px solid currentColor
}
kbd {
white-space: pre;
}
}
p, hr {
margin-block: 2.25rem;
}
hr {
border: 0;
border-block-end: 1px solid black;
margin-block: 5rem;
}
</style>
</head>
<body>
<h2>On the useful use of cat</h2>
<p>
Some people insist that we should never use a
<code>cat</code>.
</p>
<p>
This is at best a subjective opinion, and at worst harmful bullying.
</p>
<p>
Shell pipelines are not things of static beauty, they are interactive dialogs
with the UNIX shell, chatting with it to come up ad-hoc ways of gluing together
tools to the get the at the bespoke outcomes we want.
</p>
<p>
Beginning a shell pipe with a `cat` provides us maximal flexibility during the
exploratory phase. Here is a common pattern I follow:
</p>
<pre><code>cat some-huge-file | head | ...
</code></pre>
<p>
I can fill in the `...` part through trial and error, without running it on the
entire input. <i>A la</i> test driven development.
</p>
<p>
Then, when the rest of the command feels complete, I splice out the
<code>head</code> from the pipeline using the key combinations
<kbd>Ctrl - P</kbd> (prev command), <kbd>Ctrl - A</kbd>
(beginning of line), <kbd>Alt/Opt - F</kbd> twice (to skip over the cat) and
<kbd>Alt/Opt - D</kbd> twice. Seems a lot when I spell it out, but my hands do
it instantly without me thinking.
</p>
<p>
The exact key combinations are not important, and it is perhaps confusing to
bring them into the discussion. But I wanted to show how I think of the shell
pipe concretely - as a series of steps, with the first step being an input and
the last step being an output - and in such a formulation, placing the <code>head</code>
after the <code>cat</code> comes totally naturally.
</p>
<hr>
<p>
The <code>cat | head</code> is redundant, and the following would seem equivalent:
</p>
<pre><code>head some-huge-file | ...
</code></pre>
<p>which later on, once the pipe has been constructed, can be modified to</p>
<pre><code>cat some-huge-file | ...
</code></pre>
<p>
leaving the rest unchanged. This is fine really, achieves conceptual equivalence
with the “<i>first step as the command-agnostic input</i>” way of thinking I'm
describing, and is shorter to boot.
</p>
<p>
Why don't I do it that way (`head` instead of `cat`)? Because for me it'd only
be a local maxima.
</p>
<p>
For some commands, it would indeed be the best. But I don't always construct
commands using <code>cat | head</code>. Sometimes I subconsciously know how
things are going to go, by virtue of having done similar things previously, and
while I might not necessarily construct the correct shell incantation in the
first go, I might too, and usually if there is any mistake it'll take me one or
two minor tweaks to get it right.
</p>
<p>
In such cases, my hands don't bother with the `head`. So for me, always
beginning the pipeline with a <code>cat</code> is a global maxima. I don't need
to put any conscious thought into deciding if the shell pipeline is going to be
complicated or not, and if I choose incorrectly I can still easily transition
between the two modes.
</p>
<hr>
<p>
It is fine if someone decides not to use cat <i>ever</i>, it is just a personal
preference. But what I disike are the people who go around giving the “useless
use of cat award” to insist that others also stop using it. I don't know if
these folks are just misguided, or are intentionally being an ass.
</p>
</body>
</html>