What is the use/meaning of "#!/bin/sh
" in shell scripting? Please let me know whether it is considered in the script or not as it is commented.
The sha-bang ( #!) [1] at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [2] magic number, a special marker that designates a file type, or in this case an executable shell script (type man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (the line following the sha-bang line), and ignoring comments. [3]
Source: http://tldp.org/LDP/abs/html/sha-bang.html#MAGNUMREF
A script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell. /bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell
It is the path where the shell executable file is located.
It is used by the OS for regular files which have the execution bit set. If the file is not recognized as a "pure" binary format (ie, ELF, or DWARF, or other), the OS tries and reads #!/path/to/interpreter -plus -options
and transforms:
myscript myargument
into:
/path/to/interpreter -plus -options myscript myargument
It also arranges for argc and argv to be correct (ie, argv[0] will be your script name, and argv[1] being option 1 etc).
This also works for perl scripts, python scripts and whatnot. In fact, for whatever interpreter of your choice.
it means your script will run in compatibility-mode (POSIX) when executed directly (even if /bin/sh
is an alias to bash)
citations for the downvoters:
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.
http://en.wikipedia.org/wiki//bin/sh
and:
Shell scripts written with Bash-specific features (bashisms) will not function on a system using the Bourne shell or one of its replacements, unless Bash is also installed and the script begins with a "shebang line" of #!/bin/bash interpreter directive instead of #!/bin/sh.
/bin/sh
isn't actually the Bourne-sh shell in POSIX-compatible systems it means "any POSIX-compilant shell" and it's part of the specification, when you use it you're indicating that you're writing POSIX-sh code regardless of the shell used because in the case of bash it runs in compatibility mode (other shells may behave differently but the only assurance of that shebang is that you have and do POSIX). –
Bedel /bin/sh
is somehow magic, and causes a bourne-compatible shell to be run by the kernel, irrespective of where /bin/sh
is linked to. No: if /bin/sh
is symlinked to bash, then it is bash that is run, but bash spots that it's been invoked under the name of sh
, and in consequence runs in a compatibility mode, where it acts like historical/POSIX sh
. –
Vaniavanilla /bin/sh
is an element of the filesystem like any other but it doesn't necesarily mean that it's any shell in particular and specially doesn't mean it has to be Bourne-sh, it could be or it could be something else (it may not even exist) thus you can't assume anything about it other than POSIX-sh features and only if you're in a POSIX-compilant environment because POSIX requires that. –
Bedel © 2022 - 2024 — McMap. All rights reserved.