Here are the basics of BASH script that is compact to remember and versatile to carry out complicated works. These are:
Before we start, you should know some very basic practice for BASH script across all the following topics.
In UNIX system, any executable must have the "execute" permission. This includes the new bash script as well. Without the "execute" permission, you can't run the shell script.
Hence, a good practice for creating a shell script is to:
Here's the example of the commands, say we name our script demo.sh
:
holloway:Desktop$ touch demo.sh # create the empty file
holloway:Desktop$ chmod +x demo.sh # grant execute permission to file
holloway:Desktop$ ls # check the file
demo.sh
holloway:Desktop$
Throughout the following sub-chapters, when we say "create a bash script", we simply mean these 3 steps instructions.
Since this is a shell script, bash script allows .sh
extension. If you need to be explicit, you can use .bash
.
NOTE: In UNIX system, file extension doesn't matters. What really matters is the SHEBANG line inside the script, which we will cover in the sub-topic.
The first line of any executable scripts must always be the SHEBANG line. This is a special line that tells the operating system how to execute the script. In our case, we wants specifically /bin/bash
to run:
#!/bin/bash
NOTE:
#!
for the first 2 characters. It instructs the operating system to execute the program using that particular command.Now that you know the fundamental and very basic of creating, naming the bash script. We can start writing the BASH script. Please proceed to to the next sub-topic.