Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6469163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:57:31+00:00 2026-05-25T05:57:31+00:00

How and who determines what executes when a Bash-like script is executed as a

  • 0

How and who determines what executes when a Bash-like script is executed as a binary without a shebang?

I guess that running a normal script with shebang is handled with binfmt_script Linux module, which checks a shebang, parses command line and runs designated script interpreter.

But what happens when someone runs a script without a shebang? I’ve tested the direct execv approach and found out that there’s no kernel magic in there – i.e. a file like that:

$ cat target-script
echo Hello
echo "bash: $BASH_VERSION"
echo "zsh: $ZSH_VERSION"

Running compiled C program that does just an execv call yields:

$ cat test-runner.c
void main() {
        if (execv("./target-script", 0) == -1)
                perror();
}
$ ./test-runner
./target-script: Exec format error

However, if I do the same thing from another shell script, it runs the target script using the same shell interpreter as the original one:

$ cat test-runner.bash
#!/bin/bash
./target-script

$ ./test-runner.bash 
Hello
bash: 4.1.0(1)-release
zsh: 

If I do the same trick with other shells (for example, Debian’s default sh – /bin/dash), it also works:

$ cat test-runner.dash   
#!/bin/dash
./target-script

$ ./test-runner.dash 
Hello
bash: 
zsh: 

Mysteriously, it doesn’t quite work as expected with zsh and doesn’t follow the general scheme. Looks like zsh executed /bin/sh on such files after all:

greycat@burrow-debian ~/z/test-runner $ cat test-runner.zsh 
#!/bin/zsh
echo ZSH_VERSION=$ZSH_VERSION
./target-script

greycat@burrow-debian ~/z/test-runner $ ./test-runner.zsh 
ZSH_VERSION=4.3.10
Hello
bash: 
zsh: 

Note that ZSH_VERSION in parent script worked, while ZSH_VERSION in child didn’t!

How does a shell (Bash, dash) determines what gets executed when there’s no shebang? I’ve tried to dig up that place in Bash/dash sources, but, alas, looks like I’m kind of lost in there. Can anyone shed some light on the magic that determines whether the target file without shebang should be executed as script or as a binary in Bash/dash? Or may be there is some sort of interaction with kernel / libc and then I’d welcome explanations on how does it work in Linux and FreeBSD kernels / libcs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:57:32+00:00Added an answer on May 25, 2026 at 5:57 am

    Since this happens in dash and dash is simpler, I looked there first.

    Seems like exec.c is the place to look, and the relevant functionis are tryexec, which is called from shellexec which is called whenever the shell things a command needs to be executed. And (a simplified version of) the tryexec function is as follows:

    STATIC void
    tryexec(char *cmd, char **argv, char **envp)
    {
            char *const path_bshell = _PATH_BSHELL;
    
    repeat:
    
            execve(cmd, argv, envp);
    
            if (cmd != path_bshell && errno == ENOEXEC) {
                    *argv-- = cmd;
                    *argv = cmd = path_bshell;
                    goto repeat;
            }
    }
    

    So, it simply always replaces the command to execute with the path to itself (_PATH_BSHELL defaults to "/bin/sh") if ENOEXEC occurs. There’s really no magic here.

    I find that FreeBSD exhibits identical behavior in bash and in its own sh.

    The way bash handles this is similar but much more complicated. If you want to look in to it further I recommend reading bash’s execute_command.c and looking specifically at execute_shell_script and then shell_execve. The comments are quite descriptive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an algorithm that determines the near and far intersection points between
I am writing a query that determines the number of customers holding shares in
Sorry if this became a double post. I am writing a query that determines
Is there any way to determine from within a required script, where that require
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a domain model that includes something like this: public class Customer :
My users have some Patents. On it, that's a boolean Expert who determine the
Is there a way to determine who is logged on to a particular (remote)
Who here is using WASP ( http://wasp.sourceforge.net/content/ ) to in real world applications? What
Who is responsible for writing Use Cases?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.