Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can you explain me what is purpose of shebang in scripts? I have noticed that my scripts will run without shebang. i.e. When i save my crontab file it says that scripts will be executed in i.e. /bin/sh shell. So, what is the point if I put #!/bin/ksh at the start of script?
thank you
Last edited by laki47; 10-16-2009 at 03:02 AM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
My thinking goes like this: You can write a script, in any language of your choice. Because 9 times out of 10 you do not want to leave a DOS-like extension on the end (.py, .rb, .lisp, .bash, etc.), the shebang line will tell the shell running your script which interpreter to use.
Can you explain me what is purpose of shebang in scripts? I have noticed that my scripts will run without shebang. i.e. When i save my crontab file it says that scripts will be executed in i.e. /bin/sh shell. So, what is the point if I put #!/bin/ksh at the start of script?
thank you
the shebang tells the script how it should be interpreted.
of course the script could run without this, but then you would have to specify how it is interpreted on the command line.
eg
you are running a bash script from the ksh shell.
without the shebang you would have to run the script like
Code:
ksh scriptname
because the ksh shell would interpret commands differently then bash would
Always a good idea in prod systems to specify the shebang line. If the shell is unavail, it won't run at all. If you don't specify and it defaults to another shell, you could get unexpected results eg deletions/corruptions etc.
Also, you may have version issues with shells or eg Perl/Python etc.
Can you explain me what is purpose of shebang in scripts? I have noticed that my scripts will run without shebang. i.e. When i save my crontab file it says that scripts will be executed in i.e. /bin/sh shell. So, what is the point if I put #!/bin/ksh at the start of script?
thank you
Obviously, changing the shebang line changes the interpreter for the script's content.
If I have a script named "myscript.sh" that is configured to be executable, the system will try to run it, but the first thing the system does is read the shebang line, and yes, it does matter.
Some of my scripts have this line:
Code:
#!/bin/sh
Others have this line:
Code:
#!/usr/bin/ruby -W
Others have:
Code:
#!/usr/bin/perl -W
Others have:
Code:
#!/usr/bin/python
And guess what? The system chooses the interpreter solely based on the content of the shebang line, for each script.
In my opinion, the shebang should be mandatory, really
A script meant to be run on a given interpreter will surely fail horribly in another, and that's just an aesthetic issue. But if it runs without errors in an incompatible shell it can be potentially harmful.
Most shells are supersets of the bourne shell (sh), so most sh scripts will run on *most* modern shell, but once you start using features that are exclusive to a shell it will no longer be compatible with the rest, and csh variants will mostly be incompatible with anything but themselves :P
If there's no shebang, the running shell will probably fork a copy of itself and run the script in it, without caring if it's compatible or even if it's a script file at all. Shebangs also make easy to pass parameters to an interpreter. For example, to debug a bash script you might want to add -x to the bash shebang.
But when I do i.e. "crontab -e" and then save crontab file, it says:
warning: commands will be executed using /usr/bin/sh
So, does it matter what shebang is specified in file, or commands will be executed in bourne shell anyway?
No, that message means the cron script will be executed by /bin/sh, but you should be calling scripts from cron, not putting executable content in the cron script file.
Each cron schedule line should call a script, not contain a command line with specific instructions -- in all but the simplest cases. That way, you will have the option of using any interpreter you care to use.
The topic is quite old right now, but I have a question regarding situation when no shebang is placed in the script:
Quote:
Originally Posted by i92guboj
If there's no shebang, the running shell will probably fork a copy of itself and run the script in it, without caring if it's compatible or even if it's a script file at all.
And what if we execute the shell script from within a desktop environment (i.e., directly clicking on the file containing the script)? In my version of Ubuntu, running a file with execute permission bit set opens a window with Run in Terminal, Display, Cancel and Run options available. If the first one is chosen, I suppose that the script will be executed by the default login shell (or whatever shell the gnome-terminal starts upon its execution). But what if we choose the Run option?
If you do not specify an interpreter line, the default is usually the /bin/sh. But, it is recommended that you set #!/bin/bash line.
Is there a way to find out which shell is the default one in case of shebang on my specific distribution? Or maybe this shell is equivalent to user's login one?
2. There are commands like
echo $SHELL
which tells you the default shell for your system
and
echo $0
which tells you what the current shell is.
I know that. I'd like to know if there is no shebang, will the running shell fork its child process to run commands from a script in it or use the default shebang interpreter program?
PS. Provided that this is documented somewhere and an explicit answer can be given...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.