BASH Programming

What is Proper File Extension for a Bash Script

Bash or Bourne Again Shell is one of Linux’s most essential features. It is an interpreter of shell commands that you can use to automate tasks and perform various actions. In other words, you can add any Linux command in the script and create a combination of multiple commands to automate the tasks from the terminal.

Although bash scripts don’t need any particular extension to function, they usually have the .sh extension. Many Linux users need clarification about the proper file extension for a bash script. So, this blog provides deeper insight into the right extensions for a bash script.

Scripts With No Extension

Many Linux users prefer to use the shebang (#!/bin/bash) rather than using any extension with the script. For example, let’s create a script to greet a user using the script, so first create a file using the touch command: 

touch greeting

touch-command-to-create-a-file

Now, open it with the text editor and add the shebang line with the commands: 

#!/bin/bash
echo "Hello $USER"
echo "How are you?"

greeting-script-in-linux

Once you are done, provide the executable permission to the script: 

chmod u+x greeting

providing-executable-permission-to-greeting-script

Finally, run the script, and the system will print the information: 

./greeting

execute-the-greeting-script

Bash Extensions (.sh and .bash)

.sh is the universal extension for any shell, including bash, csh, tsch, etc. It does not specify which shell you are currently using for your script. On the other hand, .bash is specifically used for the bash shell and instructs the system to run the script in the bash environment.

If you are working on bash, use the .bash extension rather than .sh, as .bash ensures that it must be run in bash. Moreover, .sh provides portability for different shells, avoiding the features restricted to specific shells and working on POSIX-compliant shells.

What is Proper File Extension for a Bash Script?

There are multiple conditions under which you can use different script extensions. For example, use the shebang if you want to go simple with no extension. However, if you want to use the bash-specific feature, please use the .bash extension and .sh when you are unsure about the shell you are using. Moreover, you can use the .sh extension if you are a beginner and want to start learning the different shell scripting. 

Wrapping Up

This was all about the most accurate explanation of a proper file extension for a bash script. We have explained three different extension approaches you can use while working on bash scripting. If you are new to shell scripting, please use .sh scripting and .bash to work around the bash environment. 

About the author

Prateek Jangid