1,885
edits
Changes
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...
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 [[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 your script:
#!/bin/bash
Will force the kernel to launch /bin/bash as the interpreter for the script. This ensures that bash will used even if the user prefers ksh, csh, or some other shell for their command-line work.
There are three requirements for a valid bash script:
# The first line of the script must be <code>#!/bin/bash</code>
# The rest of the file must contain bash commands.
# The file must be made executable (see [[Linux Permissions]]).
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 your script:
#!/bin/bash
Will force the kernel to launch /bin/bash as the interpreter for the script. This ensures that bash will used even if the user prefers ksh, csh, or some other shell for their command-line work.
There are three requirements for a valid bash script:
# The first line of the script must be <code>#!/bin/bash</code>
# The rest of the file must contain bash commands.
# The file must be made executable (see [[Linux Permissions]]).