11
Since in many languages the character #
represents a committal, what is the sense of using it?
He has some special sense?
11
Since in many languages the character #
represents a committal, what is the sense of using it?
He has some special sense?
11
Yes, it has most Linux distributions. Indicates the interpreter that should be used to run a particular program/file.
Important (Remembered by our colleague @Wtrmute):
Because the "#" character is used as a comment marker in many scripting languages, the use of shebang in most cases will not interfere with the functioning of the code; in some language interpreters that do not use a wire to start comments (such as Scheme) can ignore the shebang line, in recognition of its purpose in some systems.
Giving the example with python, if you put in the first line of your prog.py
#!/usr/bin/env python
you can run this on the terminal just by doing ./prog.py
(provided you give permissions for the system to run) where the first argument to enter the command #!/usr/bin/env python
will be the file name itself, ie the command that at the bottom will happen is /usr/bin/env python prog.py
and do not need to explain python prog.py
at the terminal (where python
in this case it is only one alias for /usr/bin/env python
).
This answer from codegolf perfectly clarifies what I say.
Let’s do this for example and clarification:
Create the file ex.txt
with the content "#!/bin/rm
", and give the necessary permissions:
$ echo '#!/bin/rm' > ex.txt && chmod 777 ex.txt
Our ex.txt
has at this time only and only (but could have what we want next) in the first line:
#!/bin/rm
Then we run the "program":
$ ./ex.txt
Nothing happened (we executed our "program").
Now let’s run it again:
$ ./ex.txt
bash: ./ex.txt: No such file or directory
What happened here was:
$ /bin/rm ex.txt
In which /bin/rm
(that 'enters' as shebang), remove/delete the file/directory that comes as argument from the command, which is what you have the shebang defined/declared in its content.
It should be mentioned that the character #
was chosen to start the sequence exactly for being a comment character in various languages interpreted at the time; thus this first line (which brings information to the shell, not for the interpreter) would naturally be ignored by the latter.
Thank you @Wtrmute, I’ll mention this, it’s really important.
@Wtrmute is still a comment character in some languages such as PHP, Python and R.
@Nottherealhemingway: Of course, but newer languages cannot influence the mechanism; only those that were already used in a UNIX environment at the time shebang was invented (which apparently was in 1980).
Great example of shebang with rm
@Jeffersonquesado the merit was not mine: https://codegolf.stackexchange.com/questions/28672/code-that-will-only-execute-once/28685#28685, I only developed a little more. But obnoxious
4
In the case of scripting languages in Linux distributions, a script can start with the shebang
which is the #!
, plus the interpreter path to be used in the script ex.: #!/bin/bash
.
Using an example with shell script, I can create a file script.sh
where in the first line I place the shebang
indicating /bin/bash
as the interpreter.
#!/bin/bash
echo "Olá mundo"
When executing it after giving this execution permission (chmod a+x script.sh
) will be verified which interpreter is indicated in shebang
and the script will be executed internally as follows:
/bin/bash script.sh
For #
be used as comment indication in various scripting languages, the use of shebang
will not cause problems in the functioning of the code in most cases.
References:
Linux Shell Scripting Cookbook
Shebang - Wikipedia, the free encyclopedia
Browser other questions tagged terminology operating-system unix
You are not signed in. Login or sign up in order to post.
Related: Difference from #! in the first line of a Python script
– rray
TL;DR https://www.in-ulm.de/~mascheck/Various/shebang/
– Woss