Use .sh or .bash extension for bash scripts?

60,543

Solution 1

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

Solution 2

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.

Share:
60,543

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant almost 2 years

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

    • muru
      muru over 9 years
      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.
    • Teemu Leisti
      Teemu Leisti almost 3 years
  • Stephen Kitt
    Stephen Kitt over 9 years
    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.)
  • Léo Léopold Hertz 준영
    Léo Léopold Hertz 준영 about 9 years
    Use #!/usr/bin/env bash for portability, discussed here stackoverflow.com/a/10383546/54964
  • RolfBly
    RolfBly over 7 years
    @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.
  • user3299406
    user3299406 over 6 years
    @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.