Let's take a look at different ways of running bash scripts! The most common one is to add the execution 1
permission to the script file and run it using absolute path or ./ followed by the script 2
name if the script is in the current directory, 3
so like this; or using its absolute path home /student /scripts and the name of the script file; it's the 4
same! Note that running the script using just its name will give a "command not found" error 5
because the shell is not looking for the script in the current directory. 6
In these examples, the script was parsed and run by the program indicated by the shebang directive 7
or by bash by default, if no, shebang directive is indicated. 8
I remind you that this line indicates the shebang directive. 9
Another way to run a script is to write the name of the program or interpreter that will run the script and 10
the script name like bash first_script.sh or python3 and the name of the script 11
myscript1. Note that in this case, it's not mandatory for the script to have the execution permission 12
set. 13
So if I am removing the execution permission, I can still run the script this way. 14
The file doesn't have the execution permission, but I can run it as bash and the name of the file, 15
but not like this. 16
I'll get a permission denied error 17
I don't have the permission to execute the file because there is no execution permission. 18
This way of running a script will also overwrite the shebang directive. 19
I'm setting the execution permission again. 20
And finally, the last way of running a script is by using the source built-in command and the name 21
of the script like source first_script.sh 22
The file does not require the execution permission and the command reads and executes commands from the 23
file specified as its argument in the current shell environment. 24
The difference between the first method, so using ./ and the name of the file 25
and this one is that using first method so ./ the script will be executed in a new shell, 26
in a subshell and when using the source command, the script is running in the current shell that's running in the 27
terminal. 28
Note that source and the name of the script is the same as . a white space and the name of the script. 29
It's the same. 30
OK, we've seen several ways of running shell scripts! If you are not sure how you should run your own 31
scripts, then use the first method, which is the most common one; add the execution permission to 32
the script for the user category that you run the script, move to the script's directory 33
and run it using ./ followed by the name of the script.