For executable scripts, the system expects in the first line a so-called shebang that indicates which 1
program to run as interpreter with the script file its argument or in simpler words, what program 2
will run the file: the Bash shell, Python, PHP or other interpreter. If we execute a script without such a 3
a shebang directive, like we did in the example from the previous lecture, 4
it will use your default shell, which is Bash. 5
So in fact this script will be run by Basch or in the Bash Shell. 6
This shebang consists of a sequence of two characters: a hash and an exclamation mark, and then follows 7
the path to the interpreter that will run the file. 8
Let's change the script from the previous lecture and add that shebang on the first line. 9
First, let's see the path to bash: which basch 10
This is the path! 11
Traditionally, basch is found in /bin, but in this version of Ubuntu, we can find it in 12
both usr/bin and /bin. In fact they are hard links, 13
so a name to the same file. 14
See that they have the same inote number, so it's the same file in fact. 15
I am adding the shebang as the first line in the script. 16
So #! and the path to the bash chell so /bin/bash or usr/bin/bash it is the same 17
This is the shebang directive. 18
If I run the script again, it will run just as before because bash is the default shell anyway. 19
I'm saving the file and running it; and it is running just like before. But if I create a script in another 20
programming language, the shebang directive is mandatory to be able to run the script using its name. 21
Let's create a simple Python script. 22
Don't focus on the Python code, but only on how shebang works. 23
First, I'm not adding the shebang directive. 24
So vim let's say myscript1 25
And I'm writing two lines of Python code, so import sys and print and between parentheses sys. 26
version 27
This is the script! 28
I'm saving the file, 29
setting the execution permission and running it. 30
And we've got some errors because that's not valid basch code and basch is the default interpreter. 31
Now I'm adding the shebang. 32
Let's see the path to the Python interpreter. 33
so which python3 34
This is the path to the interpreter and I'm adding the shebang to the script. 35
usr/bin/python3 36
I'm saving the file, exiting and running it again. 37
Perfect! 38
There is no error! 39
It is working because it was parsedandn run by Python, not by bash, and it has printed out the version 40
of the current Python interpreter. 41
Let's go ahead and talk about comments! 42
Bash ignores everything written on the line after the hash symbol. 43
The only exception to this rule is when the first line of the script starts with #! 44
which is the shebang. 45
So this is not a comment! 46
But this one is a comment! 47
This is a bash comment! When writing besh scripts, it's always a good practice to make your code clean 48
and easy to understand. 49
Adding comments to your besh scripts will save you a lot of time and effort when you look at your code 50
in the future. 51
The comments also help other system administrators, who may need to maintain the script, to understand your 52
code and its purpose. 53
Let's add some comments to our script! Comments can be added at the beginning of the line or in line with other 54
code. 55
This is a comment! 56
The blank space after the hashmark is not mandatory, but it will improve the script readability. 57
Note that unlike most of the programming languages, Bash doesn't support multiline comments, even though 58
there's something similar called here documents. 59
The simplest way to write multiline comments in Bash is to add single comments one after another. 60
And one more thing! 61
It's a good idea to display the line numbers of the script so that when you have one error, on a specific 62
line, to be able to go directly to that line. 63
so in vim last line mode 64
write set nu 65
and the line numbers are displayed. 66
And to make it persistent in a file called .vimrc in the user’s home directory add set nu 67
like this in the user's home directory .vimrc and here I'm writing set nu. Now the setting 68
will be persistent. 69
It will always display the line numbers. 70
That's all for the moment. 71
Thank you.