LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-15-2009, 07:59 AM   #1
laki47
Member
 
Registered: Feb 2009
Posts: 33

Rep: Reputation: 15
Script - shebang question


Hi!

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.
Old 10-15-2009, 09:04 AM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 10-15-2009, 09:07 AM   #3
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by laki47 View Post
Hi!

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
 
Old 10-15-2009, 07:19 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 10-15-2009, 11:44 PM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by laki47 View Post
Hi!

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.
 
Old 10-15-2009, 11:54 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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.
 
Old 10-16-2009, 01:56 AM   #7
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you all to your answers.

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?
 
Old 10-16-2009, 02:42 AM   #8
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by laki47 View Post
Thank you all to your answers.

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.
 
Old 10-16-2009, 06:46 AM   #9
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
Just to butt in here, thanks to all contributors to this thread -- great content. And thanks to OP for his question.
 
Old 04-08-2013, 11:53 AM   #10
moraxu
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

Rep: Reputation: Disabled
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 View Post
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?

Also, I've read on this website that:

Quote:
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?

Last edited by moraxu; 04-08-2013 at 11:54 AM.
 
Old 04-08-2013, 12:18 PM   #11
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,138

Rep: Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314
1. Always create a new thread, rather than restarting an old one. That way it will be shown as unanswered.

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.

Linux normally uses bash, but Arios prefers zsh. I think BSD uses sh.
 
Old 04-08-2013, 12:56 PM   #12
moraxu
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

Rep: Reputation: Disabled
Quote:
Originally Posted by DavidMcCann View Post
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...

Last edited by moraxu; 04-08-2013 at 12:57 PM.
 
Old 04-08-2013, 06:38 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The default is to use the current logged in shell, because you are calling it from there, (unless you specify a shebang).

cron obviously has its own default shell, because you don't login to run cron. If it says sh, that's what you'll get.

For your Ubuntu specific qn, you'd best ask a Ubuntu forum.

As per my old post above re PROD systems and i92guboj's post I agree that specifying shebang should really be compulsory (especially on PROD).

At the cli, if you invoke as
Code:
./mysh.sh
it'll fork a new shell.
If you source it
Code:
. ./mysh.sh

#OR
 source ./mysh.sh
it will run in your current shell, not fork a new one.

Note that this is separate to what type of shell it uses eg bash, ksh etc, as this is dictated by whether or not you've specified a shebang line.

Last edited by chrism01; 04-08-2013 at 07:52 PM. Reason: typo
 
Old 04-08-2013, 06:52 PM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
When a program tries to exec() script file that does start with a shebang line:
  1. The kernel recognizes the 16-bit magic number represented by the "#!" characters and gets the path to the interpreter from the rest of the line.
  2. The kernel executes that interpreter, handing it an open file descriptor to the script file.
When a shell tries to exec() a script file that does not start with a shebang line:
  1. The kernel does not find a supported magic number at the start of the file, and the exec() call fails with ENOEXEC.
  2. The calling shell sees the failure and assumes that the file is a script in its own language.
  3. The shell invokes a subshell of itself (often by a fork() with no exec()) to interpret the script.
When a program other than a shell tries to exec() a script file that does not start with a shebang line:
  1. The exec() call fails with ENOEXEC, as above.
  2. The program sees the failure and writes an error message to stderr.
  3. The user says, "WTF is wrong? That script works fine when I run it directly from the command line."

Last edited by rknichols; 04-08-2013 at 10:20 PM.
 
2 members found this post helpful.
Old 04-11-2013, 03:53 PM   #15
moraxu
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
When a program other than a shell tries to exec() a script file that does not start with a shebang line:
  1. The exec() call fails with ENOEXEC, as above.
  2. The program sees the failure and writes an error message to stderr.
  3. The user says, "WTF is wrong? That script works fine when I run it directly from the command line."
Then the following quote:

Quote:
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.
from here is erroneous, since the exec call always fails in that case?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shebang -e argument Shwick Linux - Software 1 10-16-2008 09:54 PM
Help with shebang hammertime1983 Programming 11 11-06-2007 02:17 AM
Stuck on sript to scp a file nazs Programming 10 04-24-2007 06:19 PM
need shell backup sript maxut Linux - Software 2 10-12-2004 10:54 AM
execute sript? cloids Linux - Software 1 01-15-2004 01:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration