Clever Geek Handbook
📜 ⬆️ ⬇️

Shebang (Unix)

Shebang - a sequence of characters

Shebang ( born shebang, sha-bang , [1] [2] [3] hashbang , [4] [5] pound-bang , [2] [6] or hash-pling [2] [7] ) - in programming a sequence of two characters: a pound and an exclamation mark (" #! ") at the beginning of the script file .

When a shebang script is executed as a program on Unix-like operating systems, the program loader considers the rest of the line after the shebang as the file name of the interpreter program . The loader launches this program and passes it the script file name with the shebang as a parameter. [8] For example, if the full name of the script file is " path/to/script " and the first line of this file:

#!/bin/sh

then the loader launches " /bin/sh " (usually a Bourne shell or a compatible command line interpreter) and passes " path/to/script " as the first parameter.

The shebang line is usually skipped by the interpreter, since the "#" character is the symbol for the beginning of comments in many scripting languages. Some interpreters that do not use the pound sign to indicate the beginning of comments (such as Scheme ) may skip the shebang string to determine its purpose. [9] Other solutions rely on a preprocessor that processes and deletes the shebang string before the rest of the script is passed to the compiler or interpreter. So, for example, InstantFPC works, which allows you to run programs written in Free Pascal , like scripts on some operating systems. [ten]

Content

Syntax

A shebang string has the following format: [8]

#! interpreter [ optional-arg ]

The interpreter must be the absolute path to the executable file of the program (if the interpreter is a script, it must also begin with a shebang). The optional optional ‑ arg must be in the format of a single argument (for portability reasons, it must not contain spaces). Space after #! is optional. [2]

Examples

Some typical shebang lines:

  • #!/bin/sh - Run the file using sh ( Bourne shell ), or another compatible shell;
  • #!/bin/csh -f - Run the file using csh ( C shell ), or another compatible shell, with disabling the execution of the .cshrc file of the user;
  • #!/usr/bin/perl -T - Execute a file using Perl in Taint checking mode;
  • #!/usr/bin/env python2 - Run the file as a Python program using an environment variable to get the path to the interpreter file.

Shebang lines may contain additional arguments that are passed to the interpreter (see the example for Perl above). However, since the handling of the arguments may differ, for portability it is better to use only one argument with no spaces inside. Further portability instructions are given below.

Purpose

Specifying an interpreter in a shebang line allows you to use script and data files as system commands, hiding implementation details from users and other programs, since it eliminates the need to specify an interpreter file on the command line in front of the script file.

Suppose the script for the Bourne shell is in the file " some / path / to / foo ", the first line of which contains:

  #! / bin / sh -x

If the user tries to execute this script file using the command line (specifying "bar" and "baz" as arguments):

  some / path / to / foo bar baz

That result will be the same as executing the command:

  / bin / sh -x some / path / to / foo bar baz

If the path " / bin / sh " is the Bourne shell interpreter program, then, as a result, "bar" and "baz" will be assigned to the positional parameters $1 and $2 interpreter, and all lines of the file " some / path / to / foo " will be executed as the commands of this interpreter. Also, since the pound symbol is the symbol for the beginning of a comment in the Bourne shell language (and in many other interpreters), the shebang line will be skipped.

However, only the interpreter determines the final processing of the shebang string. So, a script containing the following two lines will simply output both lines to standard output :

  #! / bin / cat
 Hello world!

Benefits

Compared to using the global mapping of the file extension to the interpreter application, specifying the interpreter call line in the shebang allows you to specify an interpreter that is unknown at the global level and does not require system administrator rights. Shebang also allows you to specify an interpreter individually for a file without introducing a complex concept of a namespace to map a single extension to multiple interpreters.

Portability

Shebang must specify the absolute path (or path relative to the current working directory) for the executable files. This can lead to problems for systems with a non-standard file system structure. Even for systems with fairly standard directories, it is possible that variants of the same operating system store the correct interpreter in different places. Python , for example, can be in / usr / bin / python , / usr / local / bin / python , or even in / home / username / bin / python if it was not set by the system administrator.

See also

  • CrunchBang Linux is a Linux distribution, briefly called "#!"
  • interpreter directive
  • binfmt_misc (English)
  • File association
  • fragment identifier in the URL

Notes

  1. ↑ Advanced Bash Scripting Guide (unspecified) . Date of treatment January 19, 2012.
  2. ↑ 1 2 3 4 The #! magic, details about the shebang / hash-bang mechanism (unspecified) . Date of treatment January 19, 2012.
  3. ↑ Cooper, Mendel. Advanced Bash Scripting Guide 5.3 Volume 1 . - lulu.com, November 5, 2010. - P. 5. - ISBN 978-1-4357-5218-4 .
  4. ↑ MacDonald, Matthew. HTML5: The Missing Manual . - Sebastopol, California: O'Reilly Media , 2011 .-- P. 373. - ISBN 978-1-4493-0239-9 .
  5. ↑ Lutz, Mark. Learning Python . - 4th. - O'Reilly Media , September 2009. - P. 48. - ISBN 978-0-596-15806-4 .
  6. ↑ Lie Hetland, Magnus. Beginning Python: From Novice to Professional . - Apress, October 4, 2005. - P. 21. - ISBN 978-1-59059-519-0 .
  7. ↑ Schitka, John. Linux + Guide to Linux Certification . - Course Technology, December 24, 2002. - P. 353. - ISBN 978-0-619-13004-6 .
  8. ↑ 1 2 execve (2) - Linux man page (unspecified) . Date of treatment October 21, 2010.
  9. ↑ SRFI 22
  10. ↑ InstantFPC documentation

Links

  • Details about the shebang mechanism on various Unix flavors
  • #! - the Unix truth as far as I know it (a more generic approach)
  • FOLDOC shebang article
Source - https://ru.wikipedia.org/w/index.php?title=Shebang_(Unix)&oldid=100902562


More articles:

  • Twirls
  • Hay municipality
  • Sosnovo-Mazinsky Municipal Formation
  • White goose
  • Todson Jounsson
  • Arinushkina, Evdokia Vasilievna
  • European Weightlifting Championship 1949
  • 1957 European Weightlifting Championship
  • 1959 European Weightlifting Championship
  • Plavsic, Srрan

All articles

Clever Geek | 2019