Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More About Angle Brackets in Bash

#1
More About Angle Brackets in Bash

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/more-about-angle-brackets-in-bash.png" width="1516" height="748" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/more-about-angle-brackets-in-bash.png" class="ff-og-image-inserted" /></div>
<p>In the previous article, we <a href="https://www.linux.com/blog/learn/2019/1/understanding-angle-brackets-bash">introduced the subject of angle brackets</a> (<code>&lt; &gt;</code>) and demonstrated some of their uses. Here, we’ll look at the topic from a few more angles. Let’s dive right in.</p>
<p>You can use <code>&lt;</code> to trick a tool into believing the output of a command is data from a file.</p>
<p>Let’s say you are not sure your backup is complete, and you want to check that a certain directory contains all the files copied over from the original. You can try this:</p>
<pre>
diff &lt;(ls /original/dir/) &lt;(ls /backup/dir/)
</pre>
<p><a href="https://linux.die.net/man/1/diff"><code>diff</code></a> is a tool that typically compares two text files line by line, looking for differences. Here it gets the output from two <code>ls</code> commands and treats them as if coming from a file and compares them as such.</p>
<p>Note that there is no space between the <code>&lt;</code> and the <code>(...)</code>.</p>
<p>Running that on the original and backup of a directory where I save pretty pictures, I get:</p>
<pre>
diff &lt;(ls /My/Pictures/) &lt;(ls /My/backup/Pictures/) 5d4 &lt; Dv7bIIeUUAAD1Fc.jpg:large.jpg</pre>
<p>The <code>&lt;</code> in the output is telling me that there is file (<i>Dv7bIIeUUAAD1Fc.jpg:large.jpg</i>) on the left side of the comparison (in <i>/My/Pictures</i>) that is not on the right side of the comparison (in <i>/My/backup/Pictures</i>), which means copying over has failed for some reason. If <code>diff</code> didn’t cough up any output, it would mean that the list of files were the same.</p>
<p>So, you may be wondering, if you can take the output of a command or command line, make it look like the contents of a file, and feed it to an instruction that is expecting a file, that means that in the <i>sorting by favorite actor</i> example from above, you could’ve done away with the intermediate file and just piped the output from the loop into <code>sort</code>.</p>
<p>In short, yep! The line:</p>
<pre>
sort -r &lt;(while read -r name surname films;do echo $films $name $surname ; done &lt; CBactors)</pre>
<p>does the trick nicely.</p>
<h3>Here string! Good string!</h3>
<p>There is one more case for redirecting data using angle brackets (or arrows, or whatever you want to call them).</p>
<p>You may be familiar with the practice of passing variables to commands using <code>echo</code> and a pipe (<code>|</code>). Say you want to convert a variable containing a string to uppercase characters because… I don’t know… YOU LIKE SHOUTING A LOT. You could do this:</p>
<pre>
<b>myvar="Hello World"</b> <b>echo $myvar | tr '[:lower:]' '[:upper:]'</b> HELLO WORLD</pre>
<p>The <a href="https://linux.die.net/man/1/tr"><code>tr</code></a> command <i>tr</i>anslates strings to different formats. In the example above, you are telling <code>tr</code> to change all the lowercase characters that come along in the string to uppercase characters.</p>
<p>It is important to know that you are not passing on the variable, but only its contents, that is, the string “<i>Hello World</i>“. This is called the <i>here string</i>, as in “<i>it is here, in this context, that we know what string we are dealing with</i>“. But there is shorter, clearer, and all round better way of delivering <i>here strings</i> to commands. Using</p>
<pre>
tr '[:lower:]' '[:upper:]' &lt;&lt;&lt; $myvar</pre>
<p>does the same thing with no need to use echo or a pipe. It also uses angle brackets, which is the whole obsessive point of this article.</p>
<h3>Conclusion</h3>
<p>Again, Bash proves to give you lots of options with very little. I mean, who would’ve thunk that you could do so much with two simple characters like <code>&lt;</code> and <code>&gt;</code>?</p>
<p>The thing is we aren’t done. There are plenty of more characters that bring meaning to chains of Bash instructions. Without some background, they can make shell commands look like gibberish. Hopefully, post by post, we can help you decipher them. Until next time!</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016