TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Illustrated Redirection Tutorial

171 点作者 pierremenard将近 4 年前

6 条评论

euske将近 4 年前
Re: &quot;&gt;file 2&gt;&amp;1&quot; vs. &quot;2&gt;&amp;1 &gt;file&quot;<p>I tend to think &quot;a&gt;&amp;b&quot; as an assignment to a stream: stream[a]=stream[b]. This way it&#x27;s easier to understand why the ordering matters:<p><pre><code> &gt;file 2&gt;&amp;1 means stream[1]=file; stream[2]=stream[1]; 2&gt;&amp;1 &gt;file means stream[2]=stream[1]; stream[1]=file;</code></pre>
评论 #28327221 未加载
评论 #28326441 未加载
评论 #28324619 未加载
neilv将近 4 年前
This article was pretty comprehensive, including things like the ordering of `foo 2&gt;&amp;1 | bar`.<p>I would like to offer a small difference of opinion on formatting: I encourage a space after `&gt;` and `&lt;`, before the filename argument. I guess it&#x27;s subjective, and maybe varies by person, font, syntax coloring, etc., but... I think that both helps the redirection stand out, distinguishes it descriptor duplicating syntax, and avoids reduces punctuation clumped next to each other.<p>To use one of the author&#x27;s examples:<p><pre><code> cmd arg1 arg2 &lt;myFile 3&lt;&amp;1 2&gt;&#x2F;dev&#x2F;null &gt;&amp;2 cmd arg1 arg2 &lt; myFile 3&lt;&amp;1 2&gt; &#x2F;dev&#x2F;null &gt;&amp;2 cmd arg1 arg2 \ &lt; myFile \ 3&lt;&amp;1 \ 2&gt; &#x2F;dev&#x2F;null \ &gt;&amp;2 </code></pre> Keep in mind that, very often, the filenames will involve variable expressions (though I couldn&#x27;t say objectively whether that&#x27;s an argument for or against the space):<p><pre><code> foo hello --today dog &lt; &quot;$InputFile&quot; &gt; &quot;${OutputBase}.js&quot;</code></pre>
_tom_将近 4 年前
A great job of explaining file redirection to people who already understand it.<p>Unfortunately, anyone who needs it is highly unlikely to make it through the first four paragraphs.
neilv将近 4 年前
One trick that isn&#x27;t a redirection feature of the shell itself, but comes in very handy is to use&#x2F;misuse `tee` in combination with `sudo` to write files with different privileges and ownership:<p><pre><code> echo &quot;my_system_config ${MyVar}&quot; | sudo tee &#x2F;etc&#x2F;my-config-file </code></pre> And with here-documents:<p><pre><code> sudo tee &#x2F;etc&#x2F;my-config-file &lt;&lt;EOH line one line ${MyVar} line three EOH</code></pre>
评论 #28324978 未加载
punnerud将近 4 年前
To view the ASCII graphics correctly: Turn your (i)Phone and switch off Portrait Orientation Lock
cryptonector将近 4 年前
Gotta cover &gt;{varname}!