(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.
3 Answers 3
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
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.
-
7Also, 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– Stephen Kitt2015年02月04日 14:10:56 +00:00Commented Feb 4, 2015 at 14:10 -
4Use
#!/usr/bin/env bash
for portability, discussed here stackoverflow.com/a/10383546/54964Léo Léopold Hertz 준영– Léo Léopold Hertz 준영2015年06月25日 06:28:21 +00:00Commented Jun 25, 2015 at 6:28 -
9@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– RolfBly2017年01月02日 21:23:03 +00:00Commented Jan 2, 2017 at 21:23
-
2@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– Hontvári Levente2017年12月22日 16:25:57 +00:00Commented 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 GNUenv
, which makes it nonportable, and 2) you have no control over the user's$PATH
, which means you don't know whatbash
is executing it, which makes it nonportable.2023年05月23日 13:23:08 +00:00Commented May 23, 2023 at 13:23
Use the .sh file extension to write your bash scripts, but to specify the type of shell you're running, specify #!/bin/bash as the 1st line, then hit enter twice and write your code, like this:
#!/bin/bash
print(){
echo "hello" >> hello.txt
cat hello.txt
};
print
-
Not sure how this answers the question. Using the
.sh
extension may help the developer but is completely ignored by the shell. And why output to a file and then cat the file? Should you delete the file afterwards? And why append to the file instead of creating a new one? Finally,./hello.sh
andcd /tmp && ./hello.sh
could give different results.doneal24– doneal242025年02月10日 19:56:05 +00:00Commented Feb 10 at 19:56
.bash
extension. Also, it's Debian policy to have scripts in packages that land in one of thebin
folders to not have extensions.