Use .sh or .bash extension for bash scripts?

The naming of the script has nothing to do with how it's run.

The shebang line defines what interpreter is used to run the script.

I personally don't care if a script is sh, bash, perl, whatever so I just name it for what it does; I find adding an extension redundant. I'll do file scriptname to found out what the file is if I want to know that.

So if you want your script to be run with bash, use #!/bin/bash as the first line.


does using the .bash extension actually invoke bash or does it depend on system config / 1st shebang line.

If you do not use an interpreter explicitly, then the interpreter being invoked is determined by the shebang used in the script. If you use an interpreter specifically then the interpreter doesn't care what extension you give for your script. However, the extension exists to make it very obvious for others what kind of script it is.

[sreeraj@server ~]$ cat ./ext.py
#!/bin/bash
echo "Hi. I am a bash script"

See, .py extension to the bash script does not make it a python script.

[sreeraj@server ~]$ python ./ext.py
  File "./ext.py", line 2
    echo "Hi. I am a bash script"
                                ^
SyntaxError: invalid syntax

Its always a bash script.

[sreeraj@server ~]$ ./ext.py
Hi. I am a bash script