I was writing a simple shell script and found out that my shell script doesn’t require shebang line
#!/bin/sh
If I give execute permissions to my script and execute using ./myscript.sh. It runs fine.
I am using bash shell and /bin/sh is actually pointing to bash.
lrwxrwxrwx 1 root root /bin/sh -> bash
I know that shebang line is used to tell shell which interpreter to use for your rest of the script.
If I miss shebang line in perl, give execute permissions and run ./myscript.pl, it doesn’t work.
What’s actually happening here? If I use ./, When is shebang line actually needed?
shebang line is needed in the file and only if it’s meant to be run as executable (as opposed to
sh file.shinvocation. It is not actually needed by script, it is for the system to know how to find interpreter.EDIT: Sorry for misreading the question. If the shebang line is missing or not recognized,
/bin/shis used. But I prefer being explicit about the interpreter.Note, that this behavior is not universal, IIRC, only some
exec*family function do that (not to mention different platforms), so that’s another reason to be explicit here.