A brief analysis on Linux shebang symbol (#!) Tutorial

Source: Internet
Author: User
Tags hash shebang

Students who use Linux or UNIX systems may not be unfamiliar with the #! symbol, but do you really know it? This article will give you a brief introduction to Shebang ("#!" ) This symbol.

First of all, the name of the symbol (#!), translated into English, is called "shebang" or "Sha-bang" (there are other names, but I usually use these two).

Etymology and History:

Shebang's name comes from Sharp and bang, or hash bang, which refers to the typical UNIX name shebang two symbols in #!. In UNIX terminology, well numbers are often referred to as sharp,hash or mesh, while the exclamation mark is often called bang. There is also the view that the shebang name of SH comes from the name of the default shell Bourne Shell, SH, because it is often invoked using shebang.

In the 2010 version of Advanced Bash Scripting Guide (revision 6.2), Shebang was referred to as "Sha-bang", while the reference to "writing She-bang or Sh-bang" was not mentioned in the document. Shebang "This form.

Asked what he would call this feature, Dennis Ritchie replied:

Sender: "Ritchie, Dennis M (Dennis) * * CTR * * *"
The addressee:< [redacted] @talisman .org>
Date: Thu, Nov 2009 18:37:37-0600
Topic: Re:what Do-you-call your #! Line?

I don't remember we ever gave it a proper name. It's pretty late to import this feature-I think I got this inspiration from someone at the UCB Conference on Berkeley Unix; I may be one of the first to realize it, but the idea comes from someone else.

As for its name: may be similar to "Hash-bang" English wind descriptive text, but I did not use a pet-like name in any occasion to describe it.

Sincerely

Dennis

Application:

Shebang This symbol is usually written at the beginning of the first line of the script in the UNIX system, indicating the interpreter that executed the script file.

1. If the script file does not have #! this line, it will be executed by default with the current shell to interpret the script (that is, $SHELL environment variables).

2. If the interpreter after #! is an executable file, then when executing the script, it passes the filename and its arguments along as arguments to the interpreter to execute.

3. If the interpreter specified by #! does not have executable permissions, the error "Bad Interpreter:permission denied" is given. If the interpreter specified by #! is not an executable file, the specified interpreter is ignored and handed over to the current shell to execute the script.

4. If the interpreter specified by #! does not exist, the error "bad interpreter:no such file or directory" will be given. Note: After #!, the interpreter needs to write its absolute path (for example: #!/bin/bash), which does not automatically look for an interpreter in $path.

5. Of course, if you use a command such as "Bash test.sh" to execute a script, the #! line will be ignored, and the interpreter is, of course, explicitly specified in the command line bash.

An example is provided:

Take the test.sh script for example, where the code is as follows

#!/bin/bash
echo "Hello, World."
echo "Hello, ${1}."

To give the script permission to execute:
chmod a+x test.sh

You can execute the script directly using the following methods, which are actually/bin/bash./test.sh Jay
./test.sh Jay

The result returned is:
Hello, world.
Hello, Jay.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.