← Tornar a l'auditoria
Shebang
Versió de 2013 a viki.cat
·
Versió actual a ca.wikipedia
·
Historial
13.082bytes el 2013
1.842bytes avui
3.470paraules retirades
205paraules afegides
Wikitext en cru: part de les diferències són canvis de plantilles i de format,
no de contingut. Torna al text llegible.
'''Shebang''' és, en l'argot de [[Unix]],d'[[Unix]], el nom que rep el parell de caràcters[[Caràcter (tipus de dada)|caràcters]] "<tt>#!</tt>", que es troben al principi d'un [[fitxer]] de tipus [[Llenguatge script|script]]. En [[Sistema Operatiu|sistemes operatius]] de tipus Unix, la presència d'aquests dos caràcters indica que el fitxer és un ''script'' o fitxer de comandes, i que cal executar-lo utitlitzant l'[[intèrpret]] especificat a continuació, a la resta de la primera línia del fitxer. Per exemple, els fitxers [[Perl]] comencen amb la primera l'inia:
#!/opt/bin/perl
Així doncs, el '''shebang''' (també anomenat ''hashbang''<ref>[http://catb.org/jargon/html/S/shebang.html Entrada del ''Jargon File'' per shebang]</ref>) es troba sempre en la primera línia del fitxer, i consisteix en un [[coixinet]], o [[sostingut]], i un [[signe d'exclamació]], ("<tt>#!</tt>"), seguit per la ruta completa al programa intèrpret que l'executarà.
== Història ==
El shebang va ser introduit per [[Dennis Ritchie]] al Unix deals Laboratoris Bell entre les versions[[Version 7 Unix|7]] i [[Version 8 Unix|8]]. Aleshores va ser afegit al [[BSD]] de Berkeley.<ref>[http://www.in-ulm.de/~mascheck/various/shebang/sys1.c.html extracts from 4.0BSD /usr/src/sys/newsys/sys1.c]</ref> Com que la ''Versió 8'' i posteriors no es van distribuir, la primera aparició àmpliament coneguda d'aquesta característica va ser al BSD.
<!--
(also called a '''hashbang''', '''hashpling''', or '''pound bang''') refers to the characters "<tt>#!</tt>" when they are the first two characters in a [[script (computer programming)|script]] file. [[Unix-like]] [[operating system]]s take the presence of these two characters as an indication that the file is a script, and try to execute that script using the [[interpreter (computer software)|interpreter]] specified by the rest of the first line in the file. For instance, [[Shell (computing)|shell]] scripts for the [[Bourne shell]] start with the first line:
#!/bin/sh
*****************************
More precisely, a shebang line consists of a [[number sign]] and an [[exclamation point]] character ("<tt>#!</tt>"), followed by the (full) [[Path (computing)|path]] to the interpreter program that will provide the interpretation. The shebang is looked for and used when a script is invoked directly (as with a regular executable), and largely to the end of making scripts look and act similarly to regular executables, to the operating system and to the user.
Because the "<tt>#</tt>" character is often used as the comment marker in scripting languages, the contents of the shebang line will be automatically ignored by the interpreter itself; the shebang line only exists to specify the correct interpreter to be used.
== Etymology ==
The name ''shebang'' comes from an inexact [[contraction (grammar)|contraction]] of ''[[sharp (music)|SHArp]] bang'' or ''haSH bang'' {{Fact|date=February 2008}}, referring to the two typical Unix names of the two characters. Unix jargon uses ''sharp'' or ''hash'' to refer to the number sign character and ''bang'' to refer to the exclamation point, hence ''shebang''. Another theory on ''sh'' in shebang's name is from default shell <code>sh</code>, usually invoked with shebang.<ref>[http://catb.org/jargon/html/S/shebang.html Jargon File entry for shebang]</ref>
== History ==
The shebang was introduced by [[Dennis Ritchie]] between [[Version 7 Unix]] and [[Version 8 Unix|8]] at Bell Labs. It was then also added to the [[BSD]] line at Berkeley (present in 4BSD and activated by default in 4.2BSD).<ref>[http://www.in-ulm.de/~mascheck/various/shebang/sys1.c.html extracts from 4.0BSD /usr/src/sys/newsys/sys1.c]</ref> As Version 8 and later versions were not released to the public, the first widely known appearance of this feature was on BSD.
== Portability ==
Shebangs specify [[absolute path]]s to system executables; this can cause problems on systems which have non-standard file system layouts (such as [[GoboLinux]] or [[NixOS]]). Even when systems have fairly standard paths, it is quite possible for variants of the same operating system to have different locations for the desired interpreter.
In the absence of rigidly standardized locations for each interpreter, the shebang would on some systems try to execute something that doesn't exist where the shebang says it is. Therefore shebangs can limit the portability of the file.
Because of this it is not uncommon to need to edit the shebang line after copying a [[script (computer programming)|script]] from one computer to another because the path that was coded into the script may not apply on a new machine, depending on the consistency in past convention of placement of the interpreter. For this and other reasons, [[POSIX]] does not standardize the feature.
Often, the program /usr/bin/env can be used to [[#Solving shebang problems with the env program|circumvent this limitation]].
Care should be taken with this approach as while it increases portability it may introduce vulnerabilities to gain unauthorized root access.
Another portability problem is the interpretation of the command arguments.
Some systems do not split up the arguments; for example, when running the script with the first line like,
#!/usr/bin/env python -c
it will be invoked as,
/usr/bin/env "python -c"
That is, "python -c" will be passed as one argument to /usr/bin/env, rather than two arguments, "python" and "-c". On [[Linux]], this will lead to the error message,
/usr/bin/env: python -c: No such file or directory
[[Cygwin]] also behaves this way. Some other systems handle the arguments differently.
[[Plan 9 from Bell Labs]] demands a shebang when trying to execute a text file because it needs to know how to run the file. For example, a [[rc|shell script]] could have the line
#!/bin/rc
at the top. If one is forgotten, the system would say "exec header invalid."
In many [[Linux]] systems, <code>/bin/sh</code> is a [[symlink]] to <code>/bin/bash</code>, since [[bash]] implements all the commands available in the [[Bourne shell]]. Some distributions link this to other shells, however. [[Ubuntu]] (and future [[Debian]]) uses <code>/bin/dash</code>, for instance, for better performance. When script developers erroneously use the <code>#!/bin/sh</code> in scripts that contain bash-specific commands, they will fail on systems that use a different default shell. The problem can be fixed by using only [[Bourne shell|sh]]-compatible commands or by explicitly stating <code>#!/bin/bash</code> as the shebang.
===Unicode===
The Unicode [[UTF-8]] file format is backwards compatible with ASCII, and can be used where 7-bit ASCII extended into 8 bits is used, for example in Unix scripts. Some [[text editors]], like the Windows [[Notepad]], add a special unicode character, the [[byte-order mark]], to mark the document as being Unicode. It needs 3 bytes and is displayed in an ANSI editor as "" at the start of the document, and is hidden in a Unicode editor. However this violates the principle to have a Shebang as the beginning of Unix script files. Therefore this byte-order mark is not added by Unix text editors. This can give problems to read e.g. HTML files in Windows.
== As magic number ==
The shebang is actually a human-readable instance of a [[Magic number (programming)|magic number]] in the executable file, the magic byte string being <code>0x23 0x21</code>, the two characters' encoding in [[ASCII]]. (Executable files that do not require an interpreter program start with other magic combinations. See [[File format]] for more details of magic numbers.)
There have been rumours that some old versions of UNIX look for the normal shebang followed by a space and a slash ("<tt>#! /</tt>"), but this appears to be untrue.<ref>[http://www.in-ulm.de/~mascheck/various/shebang/#details 32 bit shebang myth]</ref>
On unix-like operating systems, new image files are started by the "exec" family functions. This is where the operating system will detect that an image file is either a script or an executable binary. The presence of the shebang will result in the execution of the specified (usually script language) executable. This is described on the solaris and linux man page "execve".
== Examples ==
Some typical interpreters for shebang lines:
* <code>#!/bin/bash</code> — Execute using the [[Bourne-again shell]]
* <code>#!/bin/bash -c '/bin/bash'</code> — Execute using [[bash]] in the <code>/bin/</code> directory, and calls <code>[[bash]]</code> inside the <code>/bin/</code>
* <code>#!/bin/csh</code> — Execute using <code>csh</code>, the [[C shell]]
* <code>#!/bin/ksh</code> — Execute using the [[Korn shell]]
* <code>#!/bin/awk -f</code> — Execute using [[AWK programming language|awk]] program in the <code>/bin/</code> directory; the <tt>-f</tt> is necessary; otherwise awk will think its argument is the awk program itself rather than a file that contains it
* <code>#!/bin/sh</code> — On some systems, such as [[Solaris Operating System|Solaris]], this is the [[Bourne shell]]. On [[Linux]] systems there is usually no Bourne shell and this is a link to another shell, such as bash. According to the [[Single UNIX Specification]]'s requirements for /bin/sh, such a shell will usually mimic the Bourne shell's behaviour, but be aware that using <tt>#!/bin/sh</tt> in [[shell script]]s may invoke different Bourne-compatible shells on different systems.
* <code>#!/bin/zsh</code> — Execute using <code>zsh</code>, the [[Z shell]]
* <code>#!/usr/bin/perl</code> — Execute using [[Perl]]
* <code>#!/usr/bin/php</code> — Execute using [[PHP]]. Note: since in PHP the shebang line falls outside the "code block" (<tt><?php</tt> <tt>?></tt>), it doesn't act as a comment, so PHP must explicitly ignore it.
* <code>#!/usr/bin/python</code> — Execute using [[Python (programming language)|Python]]
* <code>#!/usr/bin/lua</code> — Execute using [[Lua (programming language)|Lua]]
* <code>#!/usr/bin/env</code> — Invocation of some other program using [[env (shell)|env]] program in <code>/usr/bin</code> directory
* <code>#!/usr/bin/ruby</code> — Execute using [[Ruby (programming language)|Ruby]]
* <code>#!/usr/bin/tclsh</code> — Execute using [[Tcl|Tcl]]
* <code>#!/usr/bin/emacs --script</code> — Execute using [[Emacs|Emacs]], need Emacs 22.
Shebang lines can also include specific options that will be passed to the interpreter; see the examples below. However, implementations differ widely on how options are parsed.
This file is a [[Shell (computing)|shell]] script, the [[Unix]] equivalent of a [[DOS]] [[batch file]]:
<source lang="bash">
#!/bin/sh
echo "Hi there.";
# The rest of the shell script.
# ...
</source>
This file is an [[AWK programming language|AWK]] script:
#!/bin/awk -f
BEGIN { print "Hello, world!" }
# The rest of the AWK script.
# ...
This file is a [[Perl]] script, to be run with 'warnings' enabled (-w):
<source lang="perl">
#!/usr/bin/perl -w
print "Hello world!";
# The rest of the Perl script.
# ...
</source>
This file is also a Perl script, but it assumes that the Perl interpreter is in a different place. Also, the Perl interpreter is run without extra warnings being enabled.
<source lang="perl">
#!/usr/local/bin/perl
print "Hello again!";
# The rest of the Perl script.
#...
</source>
This is a different way of invoking a bash script, telling it to print each command before it executes it:
<source lang="bash">
#!/bin/bash -x
dosomeveryimportantprocess
# The rest of a perhaps long-running or importantly-timed Bash script.
...
</source>
This file prints itself (though it is not a [[quine (computing)|quine]] because it reads its own source code):
#!/bin/cat
Any text can be placed below the shebang.
=== Solving shebang problems with the [[env (shell)|env]] program ===
Invocation via the [[env (shell)|env]] program is useful for two purposes:
...searching for a program via the PATH environment variable:
#!/usr/bin/env python
#
# The rest of the Python script
...
...and invoking a program which is itself a <code>#!</code> script:
#!/usr/bin/env /usr/local/bin/compileAndGo "$@"
...
Note that the above shows passing of arguments which won't work if you want "compileAndGo" itself to be found by search path (case 1 above) and be aware that while this method does increase portability, it is <em>not</em> foolproof. While [[env (shell)|env]] is almost always <code>/usr/bin/env</code> a small number of platforms have it at <code>/bin/env</code>.<ref>[http://www.in-ulm.de/~mascheck/various/shebang/#details location of env]</ref>
Així doncs, el '''shebang''' (també anomenat ''hashbang''<ref>[http://catb.org/jargon/html/S/shebang.html Entrada del ''Jargon File'' per shebang]</ref>) es troba sempre en la primera línia del fitxer, i consisteix en un [[quadradet]] o [[sostingut]], i un [[signe d'exclamació]], ("<tt>#!</tt>"), seguit per la ruta completa al programa intèrpret que l'executarà.
==El Seeshebang alsova ==
*ser [[binfmtintroduït misc|binfmt_misc]]
*per [[File[[Dennis association]]]
-->Ritchie]] al Unix deals [[Laboratoris Bell]] entre les versions [[Version 7 Unix|7]] i [[Version 8 Unix|8]]. Aleshores va ser afegit al [[BSD]] de [[Berkeley]].<ref>[http://www.in-ulm.de/~mascheck/various/shebang/sys1.c.html extracts from 4.0BSD /usr/src/sys/newsys/sys1.c]</ref> Com que la ''Versió 8'' i posteriors no es van distribuir, la primera aparició àmpliament coneguda d'aquesta característica va ser al BSD.
== Referències ==
<references />{{Referències}}
== Enllaços extersexterns ==
{{Commonscat}}
* [http://www.in-ulm.de/~mascheck/various/shebang/ Details about the shebang mechanism on various Unix flavours] {{en}}
* [http://homepages.cwi.nl/~aeb/std/hashexclam.html #! - the Unix truth as far as I know it] (a more generic approach) {{en}}
* [http://foldoc.org/index.cgi?query=shebang&action=Search FOLDOC shebang article] {{en}}
[[Categoria:Unix]]