Originally Posted by john test
Thanks for your treponse. Far more illuminating than what I picked up in the search.
I will use the . form as it seems to work as well as the source form above.
Moving on to the script issues that you have raised, it seems that you are saying that if I am on ubuntu 7.10 which has bash then the shell scripts with #! /bin/bash iin the first line will fail if I move to another distro where there is a different shell???
Is that true?
If bash is not installed, yes. It will fail. And it is the way is MUST be. After all, you can't run a bash script if you don't have bash installed, just like you can't run java programs if you haven't a java virtual machine installed (I hope that the comparison makes any sense for you).
If you don't include the shebang for bash, and bash is not the default shell, then the script can fail, or have unwanted effects. The active shell will try to run it, and if the syntax is compatible then it will run without problems. This can be handy when you are using simple scripts to do simple tasks that do no rely on any specific shell feature. *Most* of the shells are supersets of sh (the bourne shell), so, if you can write what you want using sh-only code, it will ensure that your script can run on most shells. But some of them (csh comes to my mind) are very different, and that can cause lots of troubles if you try to run a script designed for another shell under them (unless the correct shebang is present, in which case the right shell will be invoked).
For now, and as a beginner, I think that it's safe enough to assume that bash will be available. Even if it's not the shell you are using, it will be installed on 99% of the linux distros by default, because many times even the init scripts depend on bash. When needed, and as instructed by the shebang, it will be called to run your script. So, usually I consider the inclussion of a correct header a good habbit that can prevent problems in the future. If a script designed for bash fails then you just know you need to install it. It's a small package in any case.
In some distros, even /bin/sh is not really sh, but a symlink to /bin/bash, you can check it with ls -l /usr/bin/sh or ls -l /bin/sh.
Also you seen to say that deleting that line altogether will cause the script to fail under some conditions. Is that true?? Any info on how the #! /bin/Xash functions to start the script.
If the needed shell doesn't exist, the script will abort with an error, which is a good thing because it will prevent your script from doing weird things. You can test yourself. Just create a file with just one line, containing "#!/bin/non_existant_shell", save it, chmod u+x it, and try to run it. It will abort telling you that the required interpreter has not been found. The same would happen for bash if it was not found.
Just to clear your doubt: as long as bash is installed, an script headed with #!/bin/bash will run without problems even if bash is not the shell you use interactively as your shell of choice.
As said above, sh would be the most portable solution. As long as you are sure you stick to the bourne shell syntax and do not use any bash (or other shell) specific stuff.