Difference between revisions of "Scripts on Linux"
Chris Tyler (talk | contribs) (New page: Linux kernels do not use file extensions such as .bat or .exe to identify the type of content in files (although some Linux applications may do so). Instead, a Linux system examines the fi...) |
Chris Tyler (talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
When an attempt is made to execute a file, the kernel will check the file [[Linux Permissions|permissions]]. If the effective user has permission to execute the file, it will be opened and the first few bytes examined for a 'magic number', which will identify the type of executable. If no magic number is found, and the file is a text file, it will be interpreted by the current shell (identified by the SHELL environment variable). | When an attempt is made to execute a file, the kernel will check the file [[Linux Permissions|permissions]]. If the effective user has permission to execute the file, it will be opened and the first few bytes examined for a 'magic number', which will identify the type of executable. If no magic number is found, and the file is a text file, it will be interpreted by the current shell (identified by the SHELL environment variable). | ||
− | If the first two characters of the file are "#!" (which is considered a magic number), then the rest of the first line of the file is used as the absolute path and arguments for the interpreter. This is called a "shbang" line, from "#" (sharp) and "!" (bang). Therefore, using this as the first line of | + | If the first two characters of the file are "#!" (which is considered a magic number), then the rest of the first line of the file is used as the absolute path and arguments for the interpreter. This is called a "shbang" line, from "#" (sharp) and "!" (bang). Therefore, using this as the first line of a [[BASH]] script: |
#!/bin/bash | #!/bin/bash | ||
− | + | For a Perl script, use: | |
− | There are three requirements for a valid | + | #!/usr/bin/perl |
+ | |||
+ | This force the kernel to execute the appropriate interpreter for the script regardless of the current shell. | ||
+ | |||
+ | There are three requirements for a valid [[BASH]] script: | ||
# The first line of the script must be <code>#!/bin/bash</code> | # The first line of the script must be <code>#!/bin/bash</code> | ||
− | # The rest of the file must contain | + | # The rest of the file must contain [[BASH]] commands. |
# The file must be made executable (see [[Linux Permissions]]). | # The file must be made executable (see [[Linux Permissions]]). | ||
+ | |||
+ | Remember, in order to execute a script, the directory containing the script must be in a directory in the [[BASH_Variables#Common_Environment_Variables|PATH]], or the script must be specified by a pathname which includes a slash (e.g., ./scriptname or an absolute path). | ||
+ | |||
+ | [[Category:Linux]] |
Latest revision as of 08:15, 16 September 2008
Linux kernels do not use file extensions such as .bat or .exe to identify the type of content in files (although some Linux applications may do so). Instead, a Linux system examines the file contents and permissions.
When an attempt is made to execute a file, the kernel will check the file permissions. If the effective user has permission to execute the file, it will be opened and the first few bytes examined for a 'magic number', which will identify the type of executable. If no magic number is found, and the file is a text file, it will be interpreted by the current shell (identified by the SHELL environment variable).
If the first two characters of the file are "#!" (which is considered a magic number), then the rest of the first line of the file is used as the absolute path and arguments for the interpreter. This is called a "shbang" line, from "#" (sharp) and "!" (bang). Therefore, using this as the first line of a BASH script:
#!/bin/bash
For a Perl script, use:
#!/usr/bin/perl
This force the kernel to execute the appropriate interpreter for the script regardless of the current shell.
There are three requirements for a valid BASH script:
- The first line of the script must be
#!/bin/bash
- The rest of the file must contain BASH commands.
- The file must be made executable (see Linux Permissions).
Remember, in order to execute a script, the directory containing the script must be in a directory in the PATH, or the script must be specified by a pathname which includes a slash (e.g., ./scriptname or an absolute path).