What is the use/meaning of “#!/bin/sh” in shell sc

2019-03-24 06:13发布

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.

标签: shell unix
5条回答
霸刀☆藐视天下
2楼-- · 2019-03-24 07:00

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

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-24 07:05

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

查看更多
smile是对你的礼貌
4楼-- · 2019-03-24 07:08

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.

查看更多
混吃等死
5楼-- · 2019-03-24 07:09

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

查看更多
Viruses.
6楼-- · 2019-03-24 07:11

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

查看更多
登录 后发表回答