<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>bash on Siddharth Arumugam - Blog</title><link>https://mrnight-blog.netlify.app/tags/bash/</link><description>Recent content in bash on Siddharth Arumugam - Blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Thu, 04 May 2023 12:30:00 +0530</lastBuildDate><atom:link href="https://mrnight-blog.netlify.app/tags/bash/index.xml" rel="self" type="application/rss+xml"/><item><title>Shebang (#!)</title><link>https://mrnight-blog.netlify.app/posts/shebang/</link><pubDate>Thu, 04 May 2023 12:30:00 +0530</pubDate><guid>https://mrnight-blog.netlify.app/posts/shebang/</guid><description>What is the shebang? The shebang (#!) is placed at the top of a script and is used to tell the kernel which interpreter to execute to read the script.
Syntax:
#!/path/to/interpreter [parameters (optional)] The name shebang or hashbang comes from # (SHArp or HASH) and ! (bang).
Using a shebang like #!/usr/bin/echo will output just the filename (linux echo command), similarly #!/usr/bin/cat will output the contents of the file (linux cat command).</description><content>&lt;h2 id="what-is-the-shebang">What is the shebang?&lt;/h2>
&lt;p>The shebang (&lt;code>#!&lt;/code>) is placed at the top of a script and is used to tell the kernel which interpreter to execute to read the script.&lt;/p>
&lt;p>Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">#!/path/to/interpreter [parameters (optional)]
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The name &lt;em>shebang&lt;/em> or &lt;em>hashbang&lt;/em> comes from # (SHArp or HASH) and ! (bang).&lt;/p>
&lt;blockquote>
&lt;p>Using a shebang like &lt;code>#!/usr/bin/echo&lt;/code> will output just the filename (linux &lt;em>echo&lt;/em> command),
similarly &lt;code>#!/usr/bin/cat&lt;/code> will output the contents of the file (linux &lt;em>cat&lt;/em> command).&lt;/p>
&lt;/blockquote>
&lt;p>Note: Shebangs max out at 127 characters.&lt;/p>
&lt;h3 id="usrbinenv-name">#!/usr/bin/env name&lt;/h3>
&lt;p>The &lt;code>/usr/bin/env&lt;/code> command looks for and runs the first executable it finds in the user&amp;rsquo;s &lt;code>$PATH&lt;/code> in a separate shell instance.&lt;/p>
&lt;ul>
&lt;li>The &lt;code>#!/usr/bin/env&lt;/code> shebang is used to increase portability of code.&lt;/li>
&lt;li>It is also used in cases where the absolute path to the interpreter is unknown or may vary (such as when handling different versions of node with the Node Version Manager (&lt;a href="https://github.com/nvm-sh/nvm">nvm&lt;/a>)).&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">#!/usr/bin/env node
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Note: Arguments cannot be passed on to the interpreter when using &lt;code>#!/usr/bin/env&lt;/code>.&lt;/p>
&lt;h3 id="why-">Why &lt;code>#!&lt;/code>?&lt;/h3>
&lt;p>The character sequence &lt;code>#!&lt;/code>is encoded to the bytes &lt;code>23 21&lt;/code>, which is the magic number of an executable script.&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup>&lt;/p>
&lt;blockquote>
&lt;p>A &lt;em>magic number&lt;/em> or &lt;em>file signature&lt;/em> is a sequence of bytes at the beginning of a file that allows to identify the type of file.
For example, a ZIP file has a file signature of &lt;code>50 4B 03 04&lt;/code>.&lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>&lt;/p>
&lt;/blockquote>
&lt;p>Ref:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://dev.to/meleu/what-the-shebang-really-does-and-why-it-s-so-important-in-your-shell-scripts-2755">What the #! shebang really does&lt;/a> by meleu&lt;/li>
&lt;li>&lt;a href="https://youtu.be/g3VxRdtlMoE">what is a shebang (#!)?&lt;/a> by anthonywritescode&lt;/li>
&lt;/ul>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>&lt;a href="https://www.in-ulm.de/~mascheck/various/shebang/4.0BSD_newsys_sys1.c.html">An email from Dennis Ritchie on #!&lt;/a>, 1980&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>&lt;a href="https://en.wikipedia.org/wiki/List_of_file_signatures">List of file signatures&lt;/a>&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></content></item></channel></rss>