Visit Our UNIX and Linux User Community


Scripts without shebang


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripts without shebang
# 1  
Old 08-30-2012
Scripts without shebang

I see lot of ad-hoc shell scripts in our servers which don't have a shebang at the beginning .

Does this mean that it will run on any shell ?

Is it a good practice to create scripts (even ad-hoc ones) without shebang ?
# 2  
Old 08-30-2012
They will run in the shell of the user who executed it.
So if user uses /bin/bash, the script will be executed in that shell.

No, that does not mean it will work in every shell, it probably won't.
# 3  
Old 08-30-2012
Quote:
Originally Posted by Peasant
They will run in the shell of the user who executed it.
So if user uses /bin/bash, the script will be executed in that shell.
That's incorrect. When an executable file's header/magic isn't recognized, it is passed to /bin/sh regardless of the user's shell.

Note that this behavior (falling back on /bin/sh) is only available through some library functions (execlp, execvp, execvpe) which wrap the execve(2) system call. Any code that tries to execute such a script using other means (such as directly invoking execve(2)) will fail.

Regards,
Alister

Last edited by alister; 08-30-2012 at 07:24 PM.. Reason: elaboration
These 9 Users Gave Thanks to alister For This Post:
# 4  
Old 08-31-2012
Note that if the script is sourced (e.g. . /path/to/script.sh) rather than run as an executable file then it will be run in the current shell.
This User Gave Thanks to CarloM For This Post:
# 5  
Old 08-31-2012
Quote:
Originally Posted by alister
That's incorrect. When an executable file's header/magic isn't recognized, it is passed to /bin/sh regardless of the user's shell.
Is that correct?
I'm on FreeBSD (sorry don't know how to get version info). The login uses bash.
If I run a script containing only this line
Code:
[[ 1 == 1 ]] && echo yes || echo no

It results in (seems to use bash)
Code:
yes

If I enter a sh-shell with sh and run the script I get (seems to use sh)
Code:
[[: not found
no

# 6  
Old 08-31-2012
Quote:
Originally Posted by CarloM
Note that if the script is sourced (e.g. . /path/to/script.sh) rather than run as an executable file then it will be run in the current shell.
True.

With regard to the original post, if the script is sourced, even if the shebang is present, it is never used.

Regards,
Alister

---------- Post updated at 10:02 AM ---------- Previous update was at 09:39 AM ----------

Quote:
Originally Posted by 244an
Is that correct?
I'm on FreeBSD...
It should be correct. It's a fundamental and standardized behavior. From your description, it sounds like you are explicitly passing the script to different shells, but I can't be certain. You did not clearly and unambiguously state the exact commands that you used to create your script and run it. Please do so.

For version info, you should be able to get it from uname -a.

Regards,
Alister

---------- Post updated at 10:24 AM ---------- Previous update was at 10:02 AM ----------

The following excerpts are from the current version of FreeBSD's libc. The most relevant bits have been highlighted:

From FreeBSD CVS - src/lib/libc/gen/exec.c
Code:
retry:		(void)_execve(bp, argv, envp);
		switch (errno) {
		case E2BIG:
			goto done;
		case ELOOP:
		case ENAMETOOLONG:
		case ENOENT:
			break;
		case ENOEXEC:
			for (cnt = 0; argv[cnt]; ++cnt)
				;
			memp = alloca((cnt + 2) * sizeof(char *));
			if (memp == NULL) {
				/* errno = ENOMEM; XXX override ENOEXEC? */
				goto done;
			}
			memp[0] = "sh";
			memp[1] = bp;
			bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
 			(void)_execve(_PATH_BSHELL,
			    __DECONST(char **, memp), envp);
			goto done;

execvpe(3) calls execve(2) (through a libc wrapper). If the system call fails with ENOEXEC, that means that an executable file was found but its format is not recognized. This happens if the file is a foreign binary or if it's a shell script without a shebang. execvpe(3) then reattempts to execve a bourne shell with the unrecognized file as its first argument.

From FreeBSD CVS - src/include/paths.h
Code:
#define	_PATH_BSHELL	"/bin/sh"

The path to the bourne shell is hardcoded in a macro.

I also took a look at glibc and they too share the same implementation design. execlp and execvp wrap execvpe. execvpe takes care of checking errno after execve, retrying with /bin/sh if appropriate. They both use _PATH_BSHELL to point to /bin/sh.

For glibc, the relevant files:
sourceware.org Git - glibc.git/blob - posix/execvpe.c
sourceware.org Git - glibc.git/blob - sysdeps/unix/sysv/linux/paths.h

Regards,
Alister
These 9 Users Gave Thanks to alister For This Post:
# 7  
Old 08-31-2012
Thank you Alister. Thanks everyone.

I have 4 quick questions:

Question 1.

According to Wikipedia /bin/sh means Bourne shell . Is that Right?

Shebang (Unix) - Wikipedia, the free encyclopedia


Question2.
So, in Solaris 10, RHEL Version5, version 6, AIX version 6, 7 , if i forget to add shebang to a script , will the script be executed using Bourne shell ?


Question3.
Regarding CarloM's post on sourcing . /path/to/script.sh
I have used sourcing only to set environmental variables in the current shell. Can sourcing be used to execute shell scripts as well?



Question4.
I have noticed that some people execute shell scripts by putting sh at the beginning like

Code:
sh /path/to/myscript.sh

I always execute shell scripts without the sh at the beginning.

Code:
/path/to/script.sh

Which is recommended ?

Previous Thread | Next Thread
Test Your Knowledge in Computers #740
Difficulty: Medium
FORTRAN II first appeared in 1958.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Necessity of shebang line

Hi , I know about the shebang line in shell scripting. Just want to know whether is there any difference in execution of the program by keeping and not keeping the shebang line. Because without shebang line also the script is working. correct me if am wrong. Any help on this will be helpful (5 Replies)
Discussion started by: rogerben
5 Replies

2. Shell Programming and Scripting

csh shebang query

What does the "-f" mean in following interpreter code #!/bin/csh -f Thank you (2 Replies)
Discussion started by: animesharma
2 Replies

3. Shell Programming and Scripting

Shebang

If i am not using #! in my script. By default where will be my script running? (6 Replies)
Discussion started by: Kochu77
6 Replies

4. Shell Programming and Scripting

Hyphen char after shebang notation

Hi, I have a trivial question to ask, I am seeing in some shell scripts the '-' (hyphen) character following the first line of shell script (i.e) the shebang notation as follows: #!/bin/sh - #! /bin/bash - what does the hyphen signify? What will happen if it is not given explicitly? (2 Replies)
Discussion started by: royalibrahim
2 Replies

5. Shell Programming and Scripting

The Shebang!

Hi, I always thought that #!/usr/bin/ksh means that the script would be executed in korn shell i.e. when we'll execute the script with this line as the very first line then the shell spawns a korn shell (in this case as we are using #!/usr/bin/ksh ) and the script gets executed. But I am... (7 Replies)
Discussion started by: dips_ag
7 Replies

6. Shell Programming and Scripting

Multiple shebang lines

*** EDIT: I found something close to my solution under an IIS 7 Module Handle.***** (Non-Homework question, simply an ease of use one) Odd question here and maybe its my newness to cgi/Perl, but is it possible to have 2 shebang lines? I write an test a ton of my homework code on my windows... (1 Reply)
Discussion started by: sennex
1 Replies

7. Shell Programming and Scripting

Relacing the shebang line of a file

Can any one tell me how to replace a shebang line of a file using sed? Eg: If a file contains the following shebang line #!C:/InstantRails/ruby/bin/ruby I would like to replace it with #!/usr/local/bin/ruby The shebang line of the file can be obtained from the command cat... (3 Replies)
Discussion started by: linuxnewbe
3 Replies

8. Shell Programming and Scripting

Doubt in shebang line!!

Do we need to include the exclamatory mark in the shebang line??:confused: What if we dont include it??:eek: Actually what shebang line implies when we run a script?? shebang line--> #!/bin/ksh :p (6 Replies)
Discussion started by: nohup
6 Replies

9. Shell Programming and Scripting

Shebang

Hi, I am currently writing BASH shell scripts. I am using BASH on a Powerbook G4 running Leopard. Could somebody please explain the difference between #!/bin/bash and #!/bin/sh? I have been using the latter (#!/bin/sh), and things have been working fine. But is that the correct one to use... (9 Replies)
Discussion started by: msb65
9 Replies

Featured Tech Videos