35

(See Use #!/bin/sh or #!/bin/bash for Ubuntu-OSX compatibility and ease of use & POSIX)

If I want my scripts to use the bash shell, does using the .bash extension actually invoke bash or does it depend on system config / 1st shebang line. If both were in effect but different, which would have precedence?

I'm not sure whether to end my scripts with .sh to just indicate "shell script" and then have the first line select the bash shell (e.g. #!/usr/bin/env bash) or whether to just end them with .bash (as well as the line 1 setting). I want bash to be invoked.

  • 3
    I don't think I have ever seen a .bash extension. Also, it's Debian policy to have scripts in packages that land in one of the bin folders to not have extensions. – muru Feb 4 '15 at 13:50
19

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
22

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.

  • 4
    Also, if the choice of implementation for the script ever changes (say it's rewritten in Python, Perl, C...), not having a .sh-style extension means there's no need to rename it. (Admittedly there's nothing preventing a C program producing a binary with a .sh extension, it would just be confusing.) – Stephen Kitt Feb 4 '15 at 14:10
  • 1
    Use #!/usr/bin/env bash for portability, discussed here stackoverflow.com/a/10383546/54964 – Léo Léopold Hertz 준영 Jun 25 '15 at 6:28
  • 3
    @wurtel (belated comment, OK?) Adding an extension imho is far from redundant. Most editors support syntax highlighting based on extension, plus it makes sense to have immediate visibility of the file type. Clarity and readabiity matter a lot. – RolfBly Jan 2 '17 at 21:23
  • @RolfBly: The editors I use recognize the shebang and provide syntax highlighting. Clarity is useful, but most of the time we want to execute a command and it is good if I have to type less and if I do not have to remember its implementation language. – Hontvári Levente Dec 22 '17 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.