What is a shebang value on Linux


Next:15.2.2 Return valuesUp:15.2 scripts Previous:15.2 scripts

Subsections


15.2.1 Running scripts

There are several methods of running scripts. The bash command can be used as the first method. It reads the specified file and executes the commands it contains. ole @ defiant: ~ / scripts> source ./werist ole

The indication says that the file is in the current working directory. To avoid this, you can insert the current directory, represented by the dot `` '', into the path.

ole @ defiant: ~ / scripts> echo $ PATH / usr / local / bin: / usr / bin: / usr / X11R6 / bin: / bin: / usr / games: / opt / gnome / bin: / opt / kde3 / bin: / opt / kde2 / bin: / usr / lib / java / bin: / opt / gnome / bin :.

Please make sure that the current directory is at the end of the list. For the system administrator root please wear never the current directory in the path.

There is also a shorter way to start a script. Just use instead of the point.

ole @ defiant: ~ / scripts>. ./werist ole

You can also start the script by calling a new instance of bash.

ole @ defiant: ~ / scripts> / bin / bash ./werist ole

However, this method behaves differently than the methods presented above. A new shell is explicitly started in which the script is then executed. The consequence of this, of course, is that variables that have not been exported are not available in this shell.

15.2.1.1 Executable file

But you can also do without an additional command. Just turn the normal text file into an executable file. Everything is executable under Linux if it has content that has been generated by the processor (native code) or by another program (interpreted code), such as B. the shell, can be executed.

To make a file executable, you have to set the X right.

ole @ defiant: ~ / scripts> ls -l werist -rw-r - r-- 1 ole users 114 Nov 20 12:48 werist ole @ defiant: ~ / scripts> chmod a + x werist ole @ defiant: ~ / scripts> ls -l werist -rwxr-xr-x 1 ole users 114 Nov 20 12:48 whoist

Now everyone (owner, group, and the rest of the world) can run this script by simply typing in the name.

ole @ defiant: ~ / scripts> werist ole Login: ole Name: Ole Vanhoefer Directory: / home / ole Shell: / bin / bash On since Wed Nov 20 10:03 (CET) on: 0, idle 200 days 20:28 , from console On since Wed Nov 20 10:04 (CET) on pts / 0, idle 5:09 On since Wed Nov 20 10:14 (CET) on pts / 1 (messages off) On since Wed Nov 20 10:53 (CET) on pts / 2, idle 2:38 (messages off) On since Wed Nov 20 12:45 (CET) on pts / 3, idle 2:19 (messages off) New mail received Thu Oct 10 11:20 2002 (CEST) Unread since Mon Jul 15 21:28 2002 (CEST) No Plan. root 2049 0.0 1.4 3408 1776? S 10:03 0:00 / usr / X11R6 / bin / xconsole -notify ole 2058 0.0 0.0 2560 0? SW 10:03 0:00 / bin / sh / usr / X11R6 / bin / kde ole 2106 0.0 0.7 19560 924? S 10:03 0:00 kdeinit: Running ... ole 2109 0.0 1.2 19544 1544? S 10:03 0:00 kdeinit: dcopserver --nosid ole 2112 0.0 2.2 21824 2788? S 10:03 0:00 kdeinit: klauncher ...

15.2.1.2 She-Bang!

There are tons of scripts on a Linux system, and there are many different languages ​​in which they can be written. If the command, the point `` '' or an executable file is used, the commands of the script are executed in the current shell. When using a separate shell for executing the script (), you can choose the environment yourself.

There is no question that it is advantageous if the script itself contained the information as to which interpreter should be used to execute the commands it contains. In Bash, this is implemented using the characters `` '' at the beginning of the first line. This construction becomes colloquial She-bang called. This word creation is made up of the terms sheepgate for the double cross `` 'and bang for the exclamation mark together.

So a script written for bash begins with the following line.

#! / bin / bash

Bash examines the first line of the script, starts the interpreter it finds and passes the script to this interpreter for execution.

She-bangShell
The Bourne Shell
The Bourne Again Shell
The Korn Shell
The C shell
The extended C shell
The streaming editor
The programming language awk
The Perl programming language

An incorrect she-bang instruction is a common cause of script execution errors. In this case, the bash and not the interpreter reports an error.

An incorrect interpreter was entered in the script started in the following example. The error message comes from bash.

ole @ enterprise: ~ / test> shebang bash: ./shebang: bad interpreter: File or directory not found

15.2.1.3 The environment

When you run a script in a new shell, a completely new instance of this shell is created. All variables that have been exported are taken from the parent shell. In addition, the configuration scripts of the respective shell are executed so that any settings can be overwritten. If your script depends on a variable, make sure that it is either set in the shell configuration file or exported as an environment variable. Please do not do both at the same time.

A basic Unix rule is that child processes inherit variables from their parent process. The variables of the child process are only valid during its execution and are not returned to the parent shell. So variable changes in this process have no effect on the variables in the parent process.

This behavior can be seen in the following example.

ole @ enterprise: ~ / test> cat shebang #! / bin / bash echo $ var var = "New value" echo $ var ole @ enterprise: ~ / test> var = "Old" ole @ enterprise: ~ / test> export var ole @ enterprise: ~ / test> shebang Alt New value ole @ enterprise: ~ / test> echo $ var Alt

Next:15.2.2 Return valuesUp:15.2 scripts Previous:15.2 scripts