What is reason behind writing
#! /bin/bash
at the very starting of every bash script ?
I means, I know that it is kind of hack but I want to know how it exactly works ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you call the script with an explicit interpreter, like
your choosen interpreter is used, no matter what the shebang says, which is just a comment.
If you make the script executable, and start it:
the kernel looks for the shebang and starts the program with the therein specified interpreter. Since the kernel doesn’t know the PATH, the whole path has to be given, or
envhas to be used:to choose the local path for a program.
Since dash, bash, zsh and so on aren’t fully compatible, you should call the program without a specific interpreter, because the author should have known what he does.
Maybe on your system
shis a link to/bin/bash, but beware! The shell can introspect how it was called, and bash can act in a way, called POSIX mode, which is slightly different and only a subset of what it normally does.If you write scripts for installation or for servers, which might run on different platforms – think OsX, Linux, Solaris and so on, where different shells are available, you will try to restrict yourself to this subset, to gain a compatible, reusable result.
But on your private machine, you might prefer the more convenient shells like
zshandbash.