{"id":453,"date":"2018-10-17T11:41:14","date_gmt":"2018-10-17T11:41:14","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/learn-the-functions-of-shebang-in-linux\/"},"modified":"2018-10-17T11:41:14","modified_gmt":"2018-10-17T11:41:14","slug":"learn-the-functions-of-shebang-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/learn-the-functions-of-shebang-in-linux\/","title":{"rendered":"Learn the Functions of Shebang in Linux"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"bash shebang linux\" height=\"341\" src=\"https:\/\/linoxide.com\/wp-content\/uploads\/2018\/09\/bash-shebang-linux-functions.png\" width=\"713\" \/><\/p>\n<p>The #! characters form a magic number. We embed these magic number in any scripts under UNIX \/ Linux operating systems to tell the kernel what interpreter to execute, to read our script. Like Linux shell, Python, Perl and R interpreters. You might have noticed all Linux shell and Perl \/ python script starts with the below line:<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>OR<\/p>\n<p>#!\/usr\/bin\/env python<\/p>\n<p>OR<\/p>\n<p>#!\/usr\/bin\/env perl<\/p>\n<p>OR<\/p>\n<p>#!\/usr\/bin\/env Rscript<\/p>\n<p>Now we will write a program file for Python language. we can execute this program by calling the interpreter directly without adding shebang line like below.<\/p>\n<h3>python_script<\/h3>\n<p>import sys&#xD;<br \/>\n&#xD;<br \/>\ndef greeting(name):&#xD;<br \/>\n sys.stdout.write(&#8220;Hello&#8221; + name + &#8220;n&#8221;)&#xD;<br \/>\n&#xD;<br \/>\nname = &#8220;Omar&#8221;&#xD;<br \/>\ngreeting(name)&#xD;<\/p>\n<p>For executing the code, we will mention python before name of the file.<\/p>\n<p>python python_script.py<br \/>\nOutput&#xD;<br \/>\n Hello Omar&#xD;<\/p>\n<p>Also we will write a program file for R language. we can execute this program by calling the interpreter directly without adding shebang line like below.<\/p>\n<h3>r_script<\/h3>\n<p>print(&#8220;hello world&#8221;)&#xD;<\/p>\n<p>For executing the code, we will mention Rscript before name of the file.<\/p>\n<p>Rscript r_script.R<br \/>\nOutput&#xD;<br \/>\n &#8220;hello world&#8221;&#xD;<\/p>\n<h2>How to make executable file<\/h2>\n<p>to make executable file we should add shebang line #!\/usr\/bin\/python to the top of script and changing the mode of the file to be executable.<\/p>\n<h3>python_script<\/h3>\n<p>#!\/usr\/bin\/python&#xD;<br \/>\nimport sys&#xD;<br \/>\n&#xD;<br \/>\ndef greeting(name):&#xD;<br \/>\n sys.stdout.write(&#8220;Hello&#8221; + name + &#8220;n&#8221;)&#xD;<br \/>\n&#xD;<br \/>\nname = &#8220;Omar&#8221;&#xD;<br \/>\ngreeting(name)&#xD;<\/p>\n<p>To make the file is executable, type the command below.<\/p>\n<p>chmod +x python_script.py<\/p>\n<p>Now we can just run the file and it will be interpreted by python.<\/p>\n<p>.\/python_script.py<br \/>\nOutput&#xD;<br \/>\n Hello Omar&#xD;<\/p>\n<h2>Large computer cluster<\/h2>\n<p>The path \/usr\/bin\/python will probably work for most default systems but might not work on things like a large computer cluster. So we will use the program env to get the right interperter.<\/p>\n<p>#!\/usr\/bin\/env python&#xD;<br \/>\nimport sys&#xD;<br \/>\n&#xD;<br \/>\ndef greeting(name):&#xD;<br \/>\n sys.stdout.write(&#8220;Hello&#8221; + name + &#8220;n&#8221;)&#xD;<br \/>\n&#xD;<br \/>\nname = &#8220;Omar&#8221;&#xD;<br \/>\ngreeting(name)&#xD;<\/p>\n<p>We can do the same for any program like Rscript as well.<\/p>\n<p> #!\/usr\/bin\/env Rscript&#xD;<br \/>\n print(&#8220;hello world&#8221;)&#xD;<\/p>\n<h3>Read Also:<\/h3>\n<ul>\n<li><a href=\"https:\/\/linoxide.com\/linux-shell-script\/shell-scripting-interview-questions-answers\/\" target=\"_blank\">70 Shell Scripting Interview Questions &amp; Answers<\/a><\/li>\n<\/ul>\n<p> <a href=\"https:\/\/linoxide.com\/linux-how-to\/what-function-shebang-linux\/\" target=\"_blank\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The #! characters form a magic number. We embed these magic number in any scripts under UNIX \/ Linux operating systems to tell the kernel what interpreter to execute, to read our script. Like Linux shell, Python, Perl and R interpreters. You might have noticed all Linux shell and Perl \/ python script starts with &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/learn-the-functions-of-shebang-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn the Functions of Shebang in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":0,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}