SheBang (#!) in linux/unix with examples and use cases

What is called SheBang(#!)

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.

Note the first line.

What is the syntax of SheBang (#!)

#!interpreter [optional-arg]

Is SheBang (#!) A comment?

Not a comment.

The purpose of SheBang (#!) ‘S line is to tell the script which command interpreter is used, which is not optional. Although SheBang (#!) Is omitted in many cases, the script can still be run, because the system uses the current command line interpreter by default in this case.

What are the specifications for SheBang (#!)

SheBang (#!) Should be on the first line of the script, and fill in the top box, otherwise it is wrong, even if the content before SheBang is a comment, this kind of error is common and difficult to find because of this When SheBang (#!) Line is actually invalid, the system uses the default command line interpreter.

How many times can SheBang (#!) Appear in the same script?

Only the first line of SheBang (#!) Will take effect, the rest will be treated as comments.

Why is this recommended: #!/usr/bin/env php

Because this is a way of porting scripts to other platforms, the default installation path of the interpreter is different in each operating system, some are /bin/, some are /usr/bin/, and may even be user-defined paths , Using env is basically universal.

Although env may also be in /bin/ or /usr/bin/, it is usually the case that there are envs in both paths, or one of them is a symbolic link to the other

SheBang (#!)  Purpose

Interpreter directives allow scripts and data files to be used as commands, hiding the details of their implementation from users and other programs, by removing the need to prefix scripts with their interpreter on the command line.

SheBang (#!) Examples

Shebang syntax features:

by#! After the opening character, there can be one or more whitespace characters, followed by the absolute path of the interpreter, which is used to call the interpreter.

Some typical shebang lines:

  • #!/bin/sh – Execute the file using the Bourne shell, or a compatible shell, assumed to be in the /bin directory
  • #!/bin/bash – Execute the file using the Bash shell
  • #!/usr/bin/env python – Execute with a Python interpreter, using the program search path to find it
  • #! /usr/bin/php – Execute with PHP’s command line interpreter

Shebang lines can also contain specific options that need to be passed to the interpreter.

SheBang (#!) Use Cases

Here is php as an example.

When we execute a script on the linux command line, there are usually several ways:

  1. Without shebang (#!), Execute: $ interpreter script.php
  2. Use shebang (#!), grant executable permissions and execute chmod + x script.php && ./script.php

Without shebang (#!)

This execution method does not specify the interpreter in the script code, but needs to be specified manually when running the script.

➜  cat scirptWithOutSB.php
<?php
	phpinfo();

➜  /usr/local/Cellar/[email protected]/7.2.24/bin/php scirptWithOutSB.php | more
phpinfo()
PHP Version => 7.2.24
...

If you do not specify the interpreter manually, the current default shell interpreter is used.

Use shebang (#!)

➜  cat scirptWithSB.php
#!/usr/local/Cellar/[email protected]/7.2.24/bin/php

<?php
	phpinfo();

➜  ./scirptWithSB.php | more

phpinfo()
PHP Version => 7.2.24
...

The above two use cases, you may have noticed, the absolute path of the interpreter, if the script is ported to other platforms to use, the absolute path of the interpreter may be different, which is why /usr/bin/env php is recommended The reason for the way.

Best linux commands articles:

Linux common commands tutorial and use examples
Sed command tutorials in linux/unix with examples and use casesAwk command tutorials in linux/unix with examples and use cases

Add a Comment

Your email address will not be published. Required fields are marked *