Hash Bang and Shell Scripting












2















Generally, shell scripts contain the following comment at the first line of the script file: #!/bin/sh. According to the researches that I made, this is called hash bang and it is conventional comment. This comment informs Unix that this file is executed by Bourne Shell scripter under the directory /bin.



My question begins in that point. Up to now I have not seen this comment like #!/bin/bash. It is always #!/bin/sh. However, ubuntu distributions do not have Bourne Shell program. They have Bourne Again Shell (bash).



In that point, is it correct to place the comment #!/bin/sh in shell scripts written in ubuntu distributions ?










share|improve this question























  • AKA shebang, en.wikipedia.org/wiki/Shebang_(Unix)

    – K7AAY
    1 hour ago
















2















Generally, shell scripts contain the following comment at the first line of the script file: #!/bin/sh. According to the researches that I made, this is called hash bang and it is conventional comment. This comment informs Unix that this file is executed by Bourne Shell scripter under the directory /bin.



My question begins in that point. Up to now I have not seen this comment like #!/bin/bash. It is always #!/bin/sh. However, ubuntu distributions do not have Bourne Shell program. They have Bourne Again Shell (bash).



In that point, is it correct to place the comment #!/bin/sh in shell scripts written in ubuntu distributions ?










share|improve this question























  • AKA shebang, en.wikipedia.org/wiki/Shebang_(Unix)

    – K7AAY
    1 hour ago














2












2








2








Generally, shell scripts contain the following comment at the first line of the script file: #!/bin/sh. According to the researches that I made, this is called hash bang and it is conventional comment. This comment informs Unix that this file is executed by Bourne Shell scripter under the directory /bin.



My question begins in that point. Up to now I have not seen this comment like #!/bin/bash. It is always #!/bin/sh. However, ubuntu distributions do not have Bourne Shell program. They have Bourne Again Shell (bash).



In that point, is it correct to place the comment #!/bin/sh in shell scripts written in ubuntu distributions ?










share|improve this question














Generally, shell scripts contain the following comment at the first line of the script file: #!/bin/sh. According to the researches that I made, this is called hash bang and it is conventional comment. This comment informs Unix that this file is executed by Bourne Shell scripter under the directory /bin.



My question begins in that point. Up to now I have not seen this comment like #!/bin/bash. It is always #!/bin/sh. However, ubuntu distributions do not have Bourne Shell program. They have Bourne Again Shell (bash).



In that point, is it correct to place the comment #!/bin/sh in shell scripts written in ubuntu distributions ?







bash shell-script shell ubuntu






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









GoktugGoktug

2739




2739













  • AKA shebang, en.wikipedia.org/wiki/Shebang_(Unix)

    – K7AAY
    1 hour ago



















  • AKA shebang, en.wikipedia.org/wiki/Shebang_(Unix)

    – K7AAY
    1 hour ago

















AKA shebang, en.wikipedia.org/wiki/Shebang_(Unix)

– K7AAY
1 hour ago





AKA shebang, en.wikipedia.org/wiki/Shebang_(Unix)

– K7AAY
1 hour ago










2 Answers
2






active

oldest

votes


















3














#!/bin/sh should work on all Unix and Unix-like distributions. It is generally thought of as the most portable hashbang so long as your script is kept POSIX compliant.





#!/bin/sh is normally just a placeholder now as the Bourne shell is no longer maintained. On many Unix systems /bin/sh will be a link to /bin/ksh, on many Linux systems it will be a link to /bin/bash (bash invoked as sh is equivalent to running bash --posix) however on Ubuntu it is a link to /bin/dash.



It is an important placeholder though because it allows for much greater portability than other methods, so long as your script is strictly POSIX compliant (repeated to stress importance).



Note: When bash is invoked in POSIX mode it will still allow some non-POSIX things like [[, arrays, and more. Those things will fail on a non-bash system.






share|improve this answer

































    1














    Yes you can use #!/bin/sh in a script because /bin/sh is (hopefully) provided for on such systems, usually via a link of some sort that makes bash behave (more or less) like a sh would. Here's a Centos7 system, for example, that links sh to bash:



    -bash-4.2$ ls -l /bin/sh
    lrwxrwxrwx 1 root root 4 Dec 4 16:48 /bin/sh -> bash
    -bash-4.2$


    You could also use #!/bin/bash if you are writing a bash script only for that system and want to use bash features. However, such scripts will suffer from portability problems, for example on OpenBSD where bash is only installed if the admin takes the trouble to install it (I do not) and then it is installed to /usr/local/bin/bash, not /bin/bash. A strictly POSIX #!/bin/sh script should be more portable.






    share|improve this answer
























    • Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

      – glenn jackman
      46 mins ago











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496505%2fhash-bang-and-shell-scripting%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    #!/bin/sh should work on all Unix and Unix-like distributions. It is generally thought of as the most portable hashbang so long as your script is kept POSIX compliant.





    #!/bin/sh is normally just a placeholder now as the Bourne shell is no longer maintained. On many Unix systems /bin/sh will be a link to /bin/ksh, on many Linux systems it will be a link to /bin/bash (bash invoked as sh is equivalent to running bash --posix) however on Ubuntu it is a link to /bin/dash.



    It is an important placeholder though because it allows for much greater portability than other methods, so long as your script is strictly POSIX compliant (repeated to stress importance).



    Note: When bash is invoked in POSIX mode it will still allow some non-POSIX things like [[, arrays, and more. Those things will fail on a non-bash system.






    share|improve this answer






























      3














      #!/bin/sh should work on all Unix and Unix-like distributions. It is generally thought of as the most portable hashbang so long as your script is kept POSIX compliant.





      #!/bin/sh is normally just a placeholder now as the Bourne shell is no longer maintained. On many Unix systems /bin/sh will be a link to /bin/ksh, on many Linux systems it will be a link to /bin/bash (bash invoked as sh is equivalent to running bash --posix) however on Ubuntu it is a link to /bin/dash.



      It is an important placeholder though because it allows for much greater portability than other methods, so long as your script is strictly POSIX compliant (repeated to stress importance).



      Note: When bash is invoked in POSIX mode it will still allow some non-POSIX things like [[, arrays, and more. Those things will fail on a non-bash system.






      share|improve this answer




























        3












        3








        3







        #!/bin/sh should work on all Unix and Unix-like distributions. It is generally thought of as the most portable hashbang so long as your script is kept POSIX compliant.





        #!/bin/sh is normally just a placeholder now as the Bourne shell is no longer maintained. On many Unix systems /bin/sh will be a link to /bin/ksh, on many Linux systems it will be a link to /bin/bash (bash invoked as sh is equivalent to running bash --posix) however on Ubuntu it is a link to /bin/dash.



        It is an important placeholder though because it allows for much greater portability than other methods, so long as your script is strictly POSIX compliant (repeated to stress importance).



        Note: When bash is invoked in POSIX mode it will still allow some non-POSIX things like [[, arrays, and more. Those things will fail on a non-bash system.






        share|improve this answer















        #!/bin/sh should work on all Unix and Unix-like distributions. It is generally thought of as the most portable hashbang so long as your script is kept POSIX compliant.





        #!/bin/sh is normally just a placeholder now as the Bourne shell is no longer maintained. On many Unix systems /bin/sh will be a link to /bin/ksh, on many Linux systems it will be a link to /bin/bash (bash invoked as sh is equivalent to running bash --posix) however on Ubuntu it is a link to /bin/dash.



        It is an important placeholder though because it allows for much greater portability than other methods, so long as your script is strictly POSIX compliant (repeated to stress importance).



        Note: When bash is invoked in POSIX mode it will still allow some non-POSIX things like [[, arrays, and more. Those things will fail on a non-bash system.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 27 mins ago









        Kusalananda

        126k16239393




        126k16239393










        answered 43 mins ago









        Jesse_bJesse_b

        12.1k23064




        12.1k23064

























            1














            Yes you can use #!/bin/sh in a script because /bin/sh is (hopefully) provided for on such systems, usually via a link of some sort that makes bash behave (more or less) like a sh would. Here's a Centos7 system, for example, that links sh to bash:



            -bash-4.2$ ls -l /bin/sh
            lrwxrwxrwx 1 root root 4 Dec 4 16:48 /bin/sh -> bash
            -bash-4.2$


            You could also use #!/bin/bash if you are writing a bash script only for that system and want to use bash features. However, such scripts will suffer from portability problems, for example on OpenBSD where bash is only installed if the admin takes the trouble to install it (I do not) and then it is installed to /usr/local/bin/bash, not /bin/bash. A strictly POSIX #!/bin/sh script should be more portable.






            share|improve this answer
























            • Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

              – glenn jackman
              46 mins ago
















            1














            Yes you can use #!/bin/sh in a script because /bin/sh is (hopefully) provided for on such systems, usually via a link of some sort that makes bash behave (more or less) like a sh would. Here's a Centos7 system, for example, that links sh to bash:



            -bash-4.2$ ls -l /bin/sh
            lrwxrwxrwx 1 root root 4 Dec 4 16:48 /bin/sh -> bash
            -bash-4.2$


            You could also use #!/bin/bash if you are writing a bash script only for that system and want to use bash features. However, such scripts will suffer from portability problems, for example on OpenBSD where bash is only installed if the admin takes the trouble to install it (I do not) and then it is installed to /usr/local/bin/bash, not /bin/bash. A strictly POSIX #!/bin/sh script should be more portable.






            share|improve this answer
























            • Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

              – glenn jackman
              46 mins ago














            1












            1








            1







            Yes you can use #!/bin/sh in a script because /bin/sh is (hopefully) provided for on such systems, usually via a link of some sort that makes bash behave (more or less) like a sh would. Here's a Centos7 system, for example, that links sh to bash:



            -bash-4.2$ ls -l /bin/sh
            lrwxrwxrwx 1 root root 4 Dec 4 16:48 /bin/sh -> bash
            -bash-4.2$


            You could also use #!/bin/bash if you are writing a bash script only for that system and want to use bash features. However, such scripts will suffer from portability problems, for example on OpenBSD where bash is only installed if the admin takes the trouble to install it (I do not) and then it is installed to /usr/local/bin/bash, not /bin/bash. A strictly POSIX #!/bin/sh script should be more portable.






            share|improve this answer













            Yes you can use #!/bin/sh in a script because /bin/sh is (hopefully) provided for on such systems, usually via a link of some sort that makes bash behave (more or less) like a sh would. Here's a Centos7 system, for example, that links sh to bash:



            -bash-4.2$ ls -l /bin/sh
            lrwxrwxrwx 1 root root 4 Dec 4 16:48 /bin/sh -> bash
            -bash-4.2$


            You could also use #!/bin/bash if you are writing a bash script only for that system and want to use bash features. However, such scripts will suffer from portability problems, for example on OpenBSD where bash is only installed if the admin takes the trouble to install it (I do not) and then it is installed to /usr/local/bin/bash, not /bin/bash. A strictly POSIX #!/bin/sh script should be more portable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            thrigthrig

            24.5k23056




            24.5k23056













            • Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

              – glenn jackman
              46 mins ago



















            • Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

              – glenn jackman
              46 mins ago

















            Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

            – glenn jackman
            46 mins ago





            Note this: "When invoked as sh, Bash enters POSIX mode after reading the startup files." -- gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

            – glenn jackman
            46 mins ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496505%2fhash-bang-and-shell-scripting%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana