Comment in bash

1

I have an Ionic v1 application, where I have some hooks to work with gulp. The hook is:

#!/bin/bash
gulp dev

When I make a ionic run android I get the error

  

"bash" is not recognized as an internal or external command,   program or batch file executable.   Error: Hook failed with error code 1: ... hooks \ before_run \ all_before_run.sh

When I draw the line #!/bin/bash everything works fine.

It is assumed that being with # is just a comment and should be ignored ... But why does it fail?

    
asked by sioesi 04.08.2017 в 16:50
source

2 answers

3
#!/bin/bash

While it is a comment, it is what is known as "shebang" . In the world unix / linux is the way to define the path to the interpreter of the script in question. In your case, the error is surely telling you that bash is not hosted in /bin , it will surely be in another folder, each system can have it in different folders, you could find out the same one doing which bash , it would not be strange that more than one in the system. When you remove this line, you are probably invoking the bash that could be configured in the path of the system. I would say do not take it off and replace it with this one:

#!/usr/bin/env bash

That basically is a little more portable and that executes the first bash that was in the path

    
answered by 04.08.2017 / 17:18
source
0

To begin with, if it worked for you when you eliminated the shebang (ie, the #! / bin / bash) it is because:

  • Your Linux distro is wrong
  • You are not in any Linux distro.
  • 1) If your Linux distro is wrong, you can run the following on your terminal.

    $command -v bash  #Para que te muestre lo que entiente tu terminal por "bash"
                      #Puedes copiar esa ruta y ponerla en el shebang.
    $cat /etc/shells  #Si no estás seguro de tener bash (lo cual sería rarísimo en tu distro)
                      #Puedes ver qué otros interpretes de comandos tienes con este comando.
    

    You can also run the script with "source", ".", "bash", "sh". And see which of these runs. "source" and "." they are synonymous and they load the content of a file in the terminal in which you are, instead, "bash" is the program that you should have in the path "/ bin / bash" and "sh" is a symbolic link to "/ bin / dash ".

    Keeping in mind that you can do something like $source tu_script

    2) In case you are not in a Linux distribution (which seems because the error you copied and hit speaks of "batch file"), install an emulator (In case you want to play with bash to scriptear ). You can install:

    Since you install it, there should be no problem with that script. I would just run it with:

    $bash tu_script
    $sh tu_script
    

    For which the shebang is not necessary. On the other hand, if you want to run it with the shebang, first make it executable with chmod +x tu_script and then run the program with:

    $./tu_script #o en su defecto.
    $/ruta/completa/a/tu_script
    

    The other is that maybe you do not need to create a file for bash, but for bat (if that is a batch processing file) and that same file put the extension .bat, you put a "pause" at the end of the code, you eliminate the shebang and should run.

    Although perhaps the best thing (only as a suggestion) is that the next one you indicate since SO, console / terminal you are running it.

    Regarding the shebang, it is related to the operation of the so-called magic numbers.

    Here is information about the shebang:

    link

    And here about the magic numbers:

    link

    But in summary, they allow a user to run a program "as if it were" a binary, read the program that describes the shebang and run the content of the program based on the program that is described, it can be bash, python, cat, awk, ruby, any of the Unix world. And it allows you to identify it, for example, when you have a program "tu_script" with the shebang "#! / Bin / bash" and you ask the command file tu_script to tell you what kind of program it is, it returns something of type:

    $file tu_script
    tu_script: Bourne-Again shell script, ASCII text executable
    
        
    answered by 06.11.2018 в 04:00