Linux has been a powerful platform for scripting languages. Therefore, shell programming or shell scripting has become one of the powerful parts of working in Linux. Of course, Windows systems too have the scripting capabilities, such as scripting in command prompt using DOS commands, but it was never able to compete with the power of Linux scripting. Linux scripting is more advanced and feature rich compared to the Windows counterpart. Linux users are capable of getting the same work done by the GUI, through the shell.
Since shell scripting is one of the main components of Linux, there are a few shell environments available for scripting. These shells are most suitable for different purposes. As an example, CSH, or in other words, C Shell, is most used for C programming.
BASH (Bourne Again Shell) is the most popular shell environment for Linux. Once you install any mainstream flavour of Linux, BASH becomes your default shell. You can optionally change this during the installation of Linux or later.
When you take BASH alone, it is quite useless for any purpose. BASH is like any other early generation programming language, but lacks the features to do something by itself. Therefore, BASH does a wise thing. Instead of facilitating all the programs or functions in BASH itself, BASH uses these programs as functions, so it can pass the parameters to the programs and obtain the outcome. For all this, BASH just has to facilitate the basic programming elements.
Originally, the shell used by UNIX was just called â€˜shellâ€™. This was mentioned as â€˜shâ€™ in the UNIX terminology. Once GNU came into the world of open source software, UNIXâ€™s â€˜shâ€™ was replaced by â€˜bashâ€™. In addition to just replacing â€˜shâ€™, bash extended the capabilities of â€˜shâ€™. First, it was Bourne Shell and then it was renamed as Bourne Again Shell (BASH) once the GNU implementation was completed.
Scripting Vs Programming
Usually, people get confused when it comes to differentiating between programming and scripting. Therefore, one might incorrectly assume that we do programming in BASH. As a matter of fact, programming languages are more powerful than the scripting languages. In addition, programming languages have better features and they are faster. As an example, Java, C, and C++ are the prime examples for programming languages. Once a programmer write the source code, the code should then be compiled in order to run on a specific platform. This is where the scripting languages become different from a programming language. For scripting languages, there is no compiler. Instead, they have interpreters that execute each instruction of the source code. Since the interpreter needs to read each instruction one-by-one, scripting languages are slower than the programming languages. When it comes BASH, it behaves same as any scripting language. The instructions of the source file are interpreted by an interpreter.
First of all, in order to start scripting in BASH, you need to know the basic commands in Linux. Most of these commands are everyday commands used in regular basis. As an example, you should have a good level of knowledge in file manipulation commands, view and edit commands, navigation commands, and searching related commands. In BASH scripting, it is all about aligning these Linux commands in a file in order to get a specific work done.
The knowledge in text editors is also one of the most important aspects of BASH scripting. For BASH script writing, you will always use one of the primary editors such as vi, gedit, nano, or emacs. Emacs is one of the most feature rich editors in Linux and it may require you to learn various shortcuts and tips before you become a pro on that. Therefore, many beginners turn to gedit or nano for fast and easy BASH scripting. Although you can start with gedit or nano, it is always a good idea to move into vi once you gather the relevant expertise. vi offers many features for fast and easy scripting.
Since you are going to practice your BASH skills, many damages can happen to the Linux system that you are working on. If you are the root user, then the damage can be irreversible. Therefore, you should never practice your BASH scripting exercises as the root user. The root user can do anything in a Linux system, including deleting the system files and devices. By practicing in root account, you might make the Linux system unusable.
Your First Time with Bash Scripting
Now we are going to write our first BASH script. Same as for any other programming langue age or scripting language, we are going to write â€˜Hello Worldâ€™ program. If you have programming experience with other languages, you should be sick writing â€˜Hello Worldâ€™ by now. Since this is the tradition, letâ€™s stick to it without changing much.
The â€˜Hello Worldâ€™ program will simply print the words â€˜Hello Worldâ€™ on the screen.
Now, go to Terminal from the menu and start a Terminal session. Usually, you will get a BASH prompt, as BASH is your default shell. Then, open your favourite text editor for writing the script. For that, you can type the following, assuming you use nano.
Once the text editor is opened, you can write your first BASH script as follows:
echo â€œHello Worldâ€
Letâ€™s try to understand what this script means. The first line tells the script to use BASH interpreter for interpreting the script. The script gives the location to the BASH interpreter as well (/bin directory). In case if your BASH interpreter is located somewhere else, you may want to do necessary changes to the first line of the script.
Indicating the interpreter is quite important for scripting. This way, you can be certain of the interpreter used for executing the script.
The second line asks the script to print the words â€œHello Worldâ€.
Now you need to save the script in a location of your file system. Letâ€™s call it test.sh.
Now, navigate to the file location and make the file executable by executing the following command.
# chmod a+x test.sh
Now it is time to run your first script.
Execute the following command and see what you get.