I’m trying to set a script that finds the location of bash then uses it in the shabang, I’m not using env because the example script I read said to use a symbolic link to env in case it’s not in usr/bin so why do that when I can just use which to find bash then set a link to it?
The problem I’m having is comparing the return of which if the command isn’t found ( -1)
Edit: I read the manual wrong which only returns -1 if no agrs are sent to it
but -1 is a newline and when I compare it is true I get the error that I’m comparing a string /bin/bash to a number -1
I’ve tried a few different ways but can’t figure out how to get it to work
newname=$(which bash)
echo $newname
fault=-1
if [ "$newname" = fault ] ; then
echo couldn\'t find bash shell
else
#! newname
You can’t do that. What’s happening now is your script is being run by the shell by default anyway. If it weren’t, how could you use the shell to get the location of the shell in a script? Even if you rename it “script.blah” and remove any shebang, as long as it is executable and contains shell commands, the shell will run it.
If you place a shebang halfway down the page, it won’t mean anything — it will just be a comment. For a shebang to be shebang,
#!needs to be the first two characters in the file. This is to tell the shell to pass off to whatever; if it doesn’t find that, it assumes it is a shell script anyway. If you want to invoke some other shell or interpreter after that, you have to do it the same way you would on the command line.