Different ways of executing shell script files.


Last Blog Review →
In the last blog we understood, the simple shell scripting basic’s which are highly necessary when we start writing the shell scripts. Understanding what is script, what is #!, how to declare variables, how to add comments.
Different ways of executing shell script files -
Execute the shell script file directly =
"bash shell_script_file_name.sh" when we run this command our file will run but wont execute.
Executing the file with format = “./filename.sh” =
"./shell_script_file_name.sh" when we run this command our file will be executed this is the proper way of executing a shell script file.
But in order to run the above command we have to give the permission to execute the file, which can be done using "chmod" command.
Ex:-
chmod 700 shell_script_file_name.sh when we change this permission then the color of the file changes from white -> green
#this permission gives executable permission to user only ./shell_script_file_name.sh
Executing shell script file as command of linux =
We can configure the shell script file to run like any other command on your system as an executable i.e. how we run "mkdir" similarly if we have a "date_and_time.sh" shell script file we can run it as a command i.e. "date_and_time" as well.
If we use this way to execute our shell script its a best practise to not name our shell script with ".sh" extension because in Linux we don't have commands ending with ".sh" extension so due to which the shell script file will look like a command.
Whenever a command is run in Linux, the OS will look at the path configured in the $PATH environment variable i.e. to locate the executable/binaries for that command. If OS can't find the command's binaries in the below path then it "command not found" error is thrown.
i-094fc5a184bbdac97 (Linux) --------------------------- ubuntu@ip-172-31-33-95:~$ sudo echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin ubuntu@ip-172-31-33-95:~$
So, in order to configure the "shell script filr" as a command i.e. to tell the Kernel that the "shell script file" is a command we have to append the path to the directory containing the script to the end of the path variable like this.
i-094fc5a184bbdac97 (Linux) --------------------------- export PATH=$PATH:/home/user-name/file_name OR export PATH=$PATH:/home/user-name/
Once this is done then we can run any non-command as command as that non-command is included in the PATH variable.
Hence we can run the "shell script file" as command without using ".sh" extension or creating a shell script file without ".sh" extension or without using any "bash" command to run the shell script file. Also make sure that the file has executable permissions that can be given by "chmod" to run the "shell script file". So, to know the location of the Linux command use : "which command-name"
Conclusion →
Using bash to execute the shell script If we use "bash filename.sh" it will just run the shell script and doesn't execute. So, even if the executable permissions are not there still the
Making the shell script as a command to run we can use it.
Running the shell script using .sh Always use ./filename.sh command to execute a shell script file, because "./filename.sh" will execute the shell script and to execute the shell script we need to give the permission using "chmod" command to execute.
Image credit - https://images.app.goo.gl/URYzuubtLLDzmwo89
Subscribe to my newsletter
Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mihir Suratwala
Mihir Suratwala
Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.