What's the difference between / bin / bash and / usr / bin / env bash?

11

I do not know much about shell script, I have always used /bin/bash , but I noticed other variations:

  • #!/usr/bin/env bash

  • #!/usr/bin/bash

  • #!/bin/bash

  • What's the difference between them?

    As far as I read this is due to portability between different systems (or kernels ), I would like to know how to call declare the script type so that it works on unix-like systems, Linux , Mac OS X and BSD     

    asked by anonymous 01.08.2016 / 18:00

    1 answer

    11

    First of all, #! in a shell script is known as shebang or hashbang . It is in the form of a comment (lines started with # are usually ignored by the shell), but the ! soon follows to indicate which is the executable that will process that script. / p>

    When you say:

    #!/usr/bin/bash
    

    or

    #!/bin/bash
    

    is calling the bash executable to interpret your script. It could be python or another interpreter.

    The problem is that from distro to distro , the place of bash (or python , for example) may be different. When you do so:

    #!/usr/bin/env bash
    

    is running the env command with the bash parameter. Env is used to create a new environment, and the following parameter is the command that will be executed by env in this environment.

    As env uses the path of the system, bash will run without you having to define your exact path.

    This article sums it up well:

      

    link


    Now, it remains to be seen if it is really true that env will always be /usr/bin :)

        
    01.08.2016 / 18:30