From bash manual:
3.7.2 Command Search and Execution
After a command has been split into words, if it results in a simple command and an optional list of arguments, the following actions are taken.
...
If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of $PATH for a directory containing an executable file by that name.
If the search is successful, or if the command name contains one or more slashes, the shell executes the named program in a separate execution environment. Argument 0 is set to the name given, and the remaining arguments to the command are set to the arguments supplied, if any.
If this execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a shell script and the shell executes it as described in Section 3.8 [Shell Scripts], page 39.
Suppose a bash script
myscript
doesn't contain a shebang.Does the quote mean that if the script is executed in bash via command
myscript
, thenbash will first assume it is an ELF and calls
execve()
on it, and because it is a bash script not ELF,execve()
call will fail,bash will next execute
bash myscript
?
Compared to running a bash script via
bash myscript
in bash, running the script viamyscript
in bash will additionally have a failure call toexecve()
on the script directly?If yes, is
myscript
slower thanbash myscript
? Why does "A Practical Guide to Linux Commands, Editors, and Shell Programming By Mark G. Sobell" say the opposite?Although you can use bash to execute a shell script, this technique causes the script to run more slowly than giving yourself execute permission and directly invoking the script.
If a bash script
myscript
contains a shebang#! /bin/bash
, when it is executed in bash via commandmyscript
,is it executed in the same way as it is executed in bash via command
bash myscript
?is it executed in the same way as the shebang were removed from the script and then the script were executed via command
myscript
?
Thanks.
My post is inspired by How is running a script like an executable different from running it by a shell explicitly? and Which shell interpreter runs a script with no shebang?