The shebang operator is used to tell which shell will be used for running the script. what will happen in the following case:
#!/bin/bash
if I enter 'ksh script_name' what will happen?
The shebang operator is used to tell which shell will be used for running the script. what will happen in the following case:
#!/bin/bash
if I enter 'ksh script_name' what will happen?
If you enter ksh script_name
, it will execute ksh
with the argument script_name
. That means it will be executed by ksh. The shebang only matters when executing the script explicitly (e.g. ./script_name
).
Short answer :
ksh will execute the script and will treat #!/bin/bash
as a comment.
Explanation :
whenever any executable is called to be executed, bash or any shell calls exec()
system call
exec()
loads the file and checks for the first 16 bits of the file to see what executable format it has. If it finds #! , it uses the rest of the first line of the file to find which program it should launch, and it provides the name of the file it was trying to launch (the script) as the last argument to the interpreter program.
and this is how interpreter specified in shebang executes the script
but when you already specified ksh and provides the argument, exec won't be searching for shebang but shell will find the binary from $PATH
variable to load the binary and gives file name as argument to the binary.