The common and widely recognized extension for a shell script is .sh. While not strictly mandatory for execution, this extension serves as a clear indicator of the file's purpose and often provides a hint for how it should be run.
Understanding Shell Script Extensions
When you encounter a file named script.sh
, the .sh
part signals that it's a shell script. This convention is particularly useful for both users and development tools.
The Role of the .sh
Extension
The .sh
extension is predominantly used for scripts intended to be executed by a Bourne shell compatible interpreter, such as sh
, bash
, or zsh
.
- User Hint: A file ending in
.sh
visually cues users that they can execute it using commands likebash script.sh
orsh script.sh
. This is an important usability feature, especially in environments where various script types (Python, Perl, etc.) coexist. - Editor Recognition: Text editors and Integrated Development Environments (IDEs) often use file extensions to automatically apply correct syntax highlighting, indentation, and code formatting, making the script easier to read and write.
- Tooling: Other development tools, linters, and build systems can use the
.sh
extension to correctly process or analyze shell scripts.
The Significance of the Shebang Line
Despite the common use of the .sh
extension, the shebang line (e.g., #!/bin/bash
or #!/bin/sh
) at the very beginning of a script is the definitive mechanism that determines which interpreter will execute the script when it's run directly (e.g., ./script.sh
).
#!/bin/bash
: Specifies that the script should be executed by the Bash interpreter.#!/bin/sh
: Specifies that the script should be executed by/bin/sh
, which is often a symbolic link tobash
,dash
, or another POSIX-compliant shell. This makes the script more portable.
Learn more about the shebang line and its importance in Unix-like systems.
When Extensions Are Not Used
It's crucial to understand that a shell script does not strictly require an extension to be executable. As long as the script has:
- A correct shebang line.
- Execute permissions set (e.g.,
chmod +x scriptname
).
It can be run directly. Many system scripts, commands, and utilities often lack extensions for a cleaner, command-like feel. For instance, a common command like ls
is effectively an executable program (or script in some minimalist systems), without any extension.
Making a Script Executable
To run a script directly, regardless of whether it has an extension, you must grant it execute permissions. This is done using the chmod
command:
chmod +x your_script_name
After granting permissions, you can execute it from your current directory using:
./your_script_name
Find out more about the chmod command and file permissions.
Common Shell Types and Extensions
While .sh
is generic, specific shells sometimes use their own extensions for clarity, though this is less common than .sh
.
Shell Type | Common Extension | Shebang Example | Notes |
---|---|---|---|
Generic | .sh |
#!/bin/sh |
Most common, implies POSIX-compliant shell. |
Bash | .sh , .bash |
#!/bin/bash |
Often uses .sh , .bash can indicate Bash-specific features. |
Korn Shell | .ksh |
#!/bin/ksh |
Less common than .sh , specific to KornShell. |
Z Shell | .zsh |
#!/bin/zsh |
Less common, specific to Zsh. |
No Extension | (none) | #!/bin/bash |
Common for system-level scripts or simple commands. |
Practical Example
Let's create a simple Bash script:
-
Create the file
hello.sh
:#!/bin/bash echo "Hello, Shell Scripting!"
-
Make it executable:
chmod +x hello.sh
-
Run the script:
./hello.sh
This will output:
Hello, Shell Scripting!
This example clearly shows how the .sh
extension provides a visual cue, while the shebang (#!/bin/bash
) ensures the correct interpreter is used after execute permissions are granted.