(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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f98a8b9c9c8b9893b98a9c8b8f9c8b">[email protected]</a> ~]$ 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.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bdcecfd8d8cfdcd7fdced8cfcbd8cf">[email protected]</a> ~]$ python ./ext.py
File "./ext.py", line 2
echo "Hi. I am a bash script"
^
SyntaxError: invalid syntax
Its always a bash script.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9cacbdcdccbd8d3f9cadccbcfdccb">[email protected]</a> ~]$ ./ext.py Hi. I am a bash script
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0