Shell How to cope with #!/bin/bash

Various scripts which originate on Linux start with #!/bin/bash which would not ordinarily exist on FreeBSD unless someone did some tinkering.

What options do people have for making this work apart from rewriting such scripts?

Is it possible to create some alias to redirect to #!/usr/local/bin/bash or create a simple /bin/bash script which redirects there?
 
The shebang calls an executable, which is located in a given path. Since the path is different than what is in your scripts already, a symlink puts a placeholder in that old location that redirects to the correct one. The symlink is created at any arbitrary time through the command line, whetehr on console or through a terminal emulator.

The correct formula that you are being offered calls a second executable that charges itself with calling bash, wheresoever it may be located.
 
Maybe not quite related to the question, but I just want to point out: in case if you _don't_ have shells/bash on your machine, there are basically two options:

1) Examine Bash-written script for sanity (actually, portability, i.e. the absence of Bashism) and if it looks good, you could just change the shebang from #!/bin/bash (or even #!/usr/local/bin/bash) to #!/bin/sh and hopefully, it should work fine. However, from my experience, people who write their scripts in Bash (usually, it happens with Linux people, because Bash is pre-installed and is a default shell in pretty much every Linux distribution), quite apt to make use of Bash-only features (namely Bashisms), which would make such scripts not portable to other shells (not all of them, but FreeBSD /bin/sh won't run it), so you almost likely would have to make some tweaks to the script, and this is how option 2) comes.

2) Rewrite the entire script (or just unportable parts of it). Yeah, I would guess this is the only option in such situation.

In any case, I suspect that every person who have ever had to make Bash (or any other feature-rich shell) script work on machine without this particular shell, would learn about importance of shell scripts being portable. I, for one, now write all my script in plain /bin/sh.
 
Back
Top