What is the use/meaning of "#!/bin/sh" in shell scripting?
Asked Answered
A

5

36

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.

Arakawa answered 8/1, 2012 at 11:41 Comment(3)
See: en.wikipedia.org/wiki/Shebang_%28Unix%29Bleach
possible duplicate of What does the line "#!/bin/sh" mean in a UNIX shell script?Busby
theunixshell.blogspot.com/search/label/shebangCercus
O
36

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

Opinionative answered 8/1, 2012 at 11:44 Comment(0)
I
10

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

Inland answered 29/12, 2016 at 8:26 Comment(0)
H
1

It is the path where the shell executable file is located.

Hulk answered 8/1, 2012 at 11:44 Comment(0)
R
0

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.

Rossanarosse answered 8/1, 2012 at 11:46 Comment(0)
B
0

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.

http://en.wikipedia.org/wiki/Bash_(Unix_shell)#Portability

Bedel answered 8/1, 2012 at 11:54 Comment(5)
It's nothing really to do with compatibility mode. That's a bash feature, not #! genericallyParfait
you're wrong, /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
You've provided two quotes, neither referencing a POSIX standard. The first link just says Bash becomes POSIX-compatible when used via #!/bin/sh. So I would claim it's still a bash feature you're describing, and not really anything generic about #!/bin/shParfait
Samus's answer is correct, but possibly not for the reason he or she thinks. The way the answer is written, it suggests that /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
ok yes, /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.