Since in many languages the #
character represents a comet, what's the point of using it?
Does it have any special meaning?
Since in many languages the #
character represents a comet, what's the point of using it?
Does it have any special meaning?
Yes, it has most of the Linux distributions. Indicates the interpreter that should be used to execute a particular program / file.
Important (Remembered in comment by colleague @Wtrmute):
Because the "#" character is used as a comment marker in many scripting languages, using shebang in most cases will not interfere with the code's operation; in some interpreters of languages that do not use a cert to initiate comments (such as Scheme) can ignore the shebang line in recognition of its purpose in some systems.
Given the example with python, if you put in the first line of your prog.py
#!/usr/bin/env python
You can run this in the terminal only by doing ./prog.py
(as long as you give the permissions to the system to execute) where the first argument to enter the command #!/usr/bin/env python
will be the file name itself, background will happen is /usr/bin/env python prog.py
and excuses to explicit python prog.py
in the terminal (where python
in this case is just a alias for /usr/bin/env python
).
This codegolf answer clarifies perfectly what I am telling you .
Let's do this for example and clarification:
We created the file ex.txt
with the content " #!/bin/rm
", and we give the necessary permissions:
$ echo '#!/bin/rm' > ex.txt && chmod 777 ex.txt
Our ex.txt
has at the moment only and only (but could have what we wanted next) in the first line:
#! / bin / rm
Then we run the "program":
$ ./ex.txt
Nothing happened (we ran our "program").
Now let's run it again:
$ ./ex.txt
bash: ./ex.txt: No such file or directory
Hehe
What happened here was:
$ /bin/rm ex.txt
In which /bin/rm
(which 'enters' as shebang ), remove / delete the file / directory that comes as command argument, which is what has the shebang in> defined / declared in your content.
In the case of scripting languages in Linux distributions, a script can start with shebang
, which is #!
, plus the interpreter path to be used in the script eg #!/bin/bash
.
Using an example with shell script, I can create a file script.sh
where in the first line I put shebang
indicating /bin/bash
as the interpreter.
#!/bin/bash
echo "Olá mundo"
Running it after giving this execute permission ( chmod a+x script.sh
) will check which interpreter is indicated in shebang
and the script will run internally as follows:
/bin/bash script.sh
Since% wc_will be used as a comment statement in various scripting languages, the use of% ww_will not cause problems with the code operation in most cases.
References:
Linux Shell Scripting Cookbook < Shebang - Wikipedia, the free encyclopedia