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 06:24 PM..
Reason: elaboration
These 9 Users Gave Thanks to alister For This Post:
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
It results in (seems to use bash)
If I enter a sh-shell with sh and run the script I get (seems to use sh)
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
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.
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.
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
I always execute shell scripts without the sh at the beginning.
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)
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)
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)
*** 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)
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)
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)
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)