48

(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 or the first 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 setting in the first line).

I want Bash to be invoked.

2
  • 4
    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, 2015 at 13:50
  • 3
    Don't use an extension. Commandname Extensions Considered Harmful. Aug 24, 2021 at 15:33

2 Answers 2

34

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 (the #!-line, which must be the first line of the script file).

On the other hand, if you use an interpreter explicitly, then the interpreter doesn't care what extension you gave 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, the .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

It's always a Bash script.

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

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 find 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.

5
  • 7
    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.) Feb 4, 2015 at 14:10
  • 3
    Use #!/usr/bin/env bash for portability, discussed here stackoverflow.com/a/10383546/54964 Jun 25, 2015 at 6:28
  • 8
    @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, 2017 at 21:23
  • 1
    @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. Dec 22, 2017 at 16:25
  • @LéoLéopoldHertz준영 The issue with the env is twofold: 1) you can not add options to the interpreter unless you are on macOS or you are using GNU env, which makes it nonportable, and 2) you have no control over the user's $PATH, which means you don't know what bash is executing it, which makes it nonportable.
    – Kusalananda
    May 23, 2023 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .