BASH - Basics

Here are the basics of BASH script that is compact to remember and versatile to carry out complicated works. These are:

  • the basic syntax
  • the techniques covering logical and needs
  • the tips in data processing, piping and integration

Before we start, you should know some very basic practice for BASH script across all the following topics.

Basic Script Creation

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:

  1. Create the empty script file
  2. Grant execute permission
  3. Edit the bash script

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.

BASH Script File Extensions

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.

SHEBANG Line

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:

  • SHEBANG line is the first line of the script. It is symbolized by #! 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.