Bash File Extension: Should a Bash Script Use .sh or No Extension?
Bash file extension usually means the suffix used in the file name of a Bash script, such as .sh. In Unix-like systems, a Bash script does not require a file extension to run. What matters for direct execution is the shebang line, executable permission, and how you invoke the file.
In this Bash Tutorial, we will learn when to use .sh, when to use no extension, why #!/bin/bash is more important than the file name extension, and how to run a Bash script correctly.
Recommended Bash Script File Naming Rule
For a script that you run like a command, using no extension is common and clean. For example, a command-like script can be named backup-files, deploy-app, or generate-report. The operating system does not need .sh to identify it as a Bash script.
For a small script, learning example, or file that you want people to recognize immediately as a shell script, .sh is also acceptable. The extension is mainly for humans and editors, not for the Unix kernel.
| File name style | Example | Best used for |
|---|---|---|
| No extension | backup-files | Executable scripts used like commands |
.sh extension | backup-files.sh | Learning files, shared examples, or simple shell scripts |
.bash extension | backup-files.bash | Occasional use when you want to make Bash-specific syntax obvious |
Bash Hash Bang
No extension is required for Bash Script File when you specify Hash Bang, #!/bin/bash , as the first line of code. The Hash Bang is a Kernel convention and helps the Kernel in deciding the interpreter. Kernel deciding the interpreter!! Whats that? The scripts can be in bash, python, perl, php, etc. And specifying the interpreter in Hash Bang is sufficient for your Kernel to execute the Script.
In this example script, we provide no extension to the script file, but mention the Hash Bang as the first line in the script file.
Bash Script File
#!/bin/bash
# echo command
echo Welcome to TutorialKart
# another echo command
echo Learn Bash Scripting
Output
~$ ls
bash-example
~$ cat bash-example
#!/bin/bash
# echo command
echo Welcome to TutorialKart
# another echo command
echo Learn Bash Scripting
~$ ./bash-example
Welcome to TutorialKart
Learn Bash Scripting
Our script file does not have any extension, and while running the script, ~$ ./bash-example, we have not mentioned any extension.
Before running a script directly with ./bash-example, make sure the file has execute permission. You can add it with chmod +x.
chmod +x bash-example
./bash-example
.sh Bash Script Extension
In this example, we do not specify Hash Bang as the first line in our script file, but use .sh extension in the file name.
Bash Script File
# echo command
echo Welcome to TutorialKart
# another echo command
echo Learn Bash Scripting
Output
~$ ls
bash-example bash-example.sh
~$ ./bash-example.sh
Welcome to TutorialKart
Learn Bash Scripting
Our script file have .sh extension, and while running the script, ~$ ./bash-example.sh, we mentioned the extension.
However, the .sh extension by itself does not select Bash as the interpreter. If a script is executed directly, the shebang line is the reliable way to say which interpreter should run the file. A file named script.sh can still contain Bash, POSIX sh, Zsh, Python, or any other script content. The extension is only a naming convention.
Use #!/bin/bash or #!/usr/bin/env bash in a Bash Script
If your script uses Bash-specific features, include a Bash shebang at the top. This avoids confusion when the script is run directly.
#!/bin/bash
Another common form is:
#!/usr/bin/env bash
#!/bin/bash uses Bash from a fixed path. #!/usr/bin/env bash asks the environment to locate Bash from the current PATH. Both are widely used. Choose one style and use it consistently in your project.
Run a Bash File With Extension or Without Extension
You can run a Bash script in different ways. The behavior depends on whether you call Bash explicitly or execute the file directly.
# Run using Bash explicitly
bash bash-example.sh
# Run directly from the current directory
./bash-example.sh
When you run bash bash-example.sh, Bash runs the file because you explicitly asked Bash to do it. The file does not need execute permission in the same way a directly executed file does, but it must be readable.
When you run ./bash-example.sh, the system executes the file directly. In that case, the file should have execute permission, and the shebang should identify the interpreter.
chmod +x bash-example.sh
./bash-example.sh
.sh vs Bash: Why the Extension Can Be Misleading
The name .sh often suggests a shell script, but it does not always mean Bash. On many systems, sh refers to a POSIX-compatible shell, while Bash is a specific shell with additional features. A script written for Bash may fail if it is run with sh.
For example, Bash arrays are not supported by plain POSIX sh. If your script uses Bash arrays, use a Bash shebang and run it with Bash.
#!/bin/bash
names=("Apple" "Banana" "Cherry")
echo "${names[1]}"
Running this as a Bash script works, but running it with sh can produce a syntax error on systems where sh is not Bash.
Syntax error: "(" unexpected
When to Use No Extension for Bash Scripts
- Use no extension when the script behaves like a command.
- Use no extension when the script will be placed in a directory such as
/usr/local/binor another directory inPATH. - Use no extension when the interpreter is already clear from the shebang.
- Use no extension when you do not want users to think they must type or care about the script language.
For example, a command named backup-files is easier to use than backup-files.sh after it is installed in your PATH.
backup-files
When to Use .sh Extension for Bash Scripts
- Use
.shfor beginner examples where the file type should be visible. - Use
.shwhen a project convention already uses it. - Use
.shfor small helper scripts that are not installed as commands. - Use
.shif your editor, file manager, or team workflow depends on extensions for recognition.
If the script uses Bash-specific syntax, still keep the Bash shebang even when the file name ends with .sh.
Bash File Extension Checklist Before Running a Script
- Check whether the script needs Bash-specific syntax or only POSIX
shsyntax. - Add
#!/bin/bashor#!/usr/bin/env bashif the script is meant for Bash. - Use
chmod +x filenamebefore running the script directly with./filename. - Do not rely on
.shto choose the interpreter. - Use no extension for command-style scripts and
.shfor simple visible script files if that helps your workflow.
Common Bash File Extension Mistakes
- Assuming
.shmeans Bash: A.shfile may be run by Bash, Dash, or another shell depending on how it is invoked. - Leaving out the shebang: Direct execution is more predictable when the interpreter is declared on the first line.
- Running Bash syntax with
sh: Bash arrays,[[ ]], and some expansions may fail under plainsh. - Forgetting execute permission: A directly executed script needs execute permission.
- Adding extensions to installed commands unnecessarily: Command-style scripts are often easier to use without extensions.
Bash File Extension FAQs
What is the extension of a Bash file?
A Bash file may use the .sh extension, but no extension is required. On Unix-like systems, the shebang line and execution method matter more than the file name extension.
Should I name my Bash script with .sh or no extension?
Use no extension when the script is meant to behave like a command. Use .sh when you want the file to be visibly recognized as a shell script, especially in examples or small helper scripts.
Does .sh automatically run a file with Bash?
No. The .sh extension does not automatically select Bash. If you run a file directly, the shebang line decides the interpreter. If you run bash script.sh, Bash runs it because you explicitly called Bash.
What do I do with a .sh file?
You can inspect it with a text editor, make it executable with chmod +x file.sh, and run it with ./file.sh. You can also run it with bash file.sh when it is a Bash script.
Should I use sh or bash to run a shell script?
Use bash if the script uses Bash-specific features. Use sh only when the script is written for POSIX sh syntax. If the file starts with #!/bin/bash, run it as a Bash script.
Concluding this, we have learned how to provide the extension to a bash script file or let the kernel pick up the interpreter with the hash bang in the script file.
Conclusion: Bash File Extension Depends on Usage, Not Execution
In this Bash Tutorial, we learned that a Bash script can use .sh or no extension. The extension is a naming convention. For reliable execution, use the correct shebang, give execute permission when needed, and run the script with the intended shell.
TutorialKart.com