Updated May 10, 2023
Introduction to Shell Scripting in Linux
The language of 0’s and 1’s, known as binary language, is generally understood by computers. Decades ago, people used to write programs and applications, like even calculators in binary languages. But that wasn’t pretty. Just imagine! You want to calculate 73+96; how would you write? You would have to rather calculate the binary integer for 73 and 96 and then add them. A person has to be pretty stupid to do that. One would rather calculate 73 and 96 on a paper and move on. But that is for the smaller part. What if you have to calculate something like (19273/23*[2967^3]). I would say now that this is pretty intense.
So, an ideal man learning to encode everything in binary language doesn’t make sense. That is the main reason why computers weren’t popular in the 70s and the 80s. Even before the development of Windows, people started developing kernels and similar components that formed the core part of the operating system. People use the kernels to understand the human-readable format of a specific program and then convert it into machine code. And this is where programming languages emerged and changed the whole era of the computer world. Applications were built in C and C++ and later on with Java, Python, Ruby, and Perl.
As of now, there are more than 50 programming languages all around. And that is at least those which I know of. Let alone the fact that there are other programming languages that are even harder to understand than machine code. My point is examples like Chicken Programming and Malbolge. They are some seriously nasty stuff to deal with.
Shell Programming….errr…Scripting?
You might wonder why I haven’t mentioned Shell programming or other programming languages above. The main reason being Shell itself is not a programming language. It is a scripting language. An interactive program called the shell interprets user input and provides results if the command is valid. After that, it passes the program to the kernel to execute it. The Shell is not a part of the Kernel; instead, it uses the kernel to execute and create files.
Types of Shell Scripting Program
Now, unlike C, C++, or Java, there are several other types of shells that can be used for Shell Scripting in linux. Following are the four types of shell available to date:
No. | Shell Type | Description |
1. | Bash, aka Bourne Again Shell | This is the most common shell available on all Linux and Debian-based systems. It is open source and freeware. |
2. | CSH or C Shell | This Shell scripting program uses the C programming’s shell syntax, and it’s almost similar to C. |
3. | KSH or Korn Shell | Korn is a Unix-based Shell scripting program, initially based on Bash Shell Scripting. The shell is a highly advanced high-level programming language. |
4. | TCSH | There is no specific fullform of TCSH. It is as it is. TCSH is an advanced version of Berkeley Unix C shell. It again supports C-style syntax. |
Since Bash is the most commonly used shell in the world, we will only focus on it today and won’t be discussing the other shells.
The Bash Shell
The Bash Shell scripting program is a high-level scripting language similar to that of Python. But if you have any experience with Python before, then you will actually find that Bash Shell Scripting is far easier than any other scripting language. One can write scripts in bash much faster than in C, C++, Java, or Python. The default terminal in today’s most Linux environments says, for e.g.:- Ubuntu, Linux Mint Cinnamon/ Rafaela, Fedora, Arch Linux, or any other Debian environment, consists of Bash as the default Shell. You can, however, check all the shell environments present in your system by typing the following in the default Terminal:-
$ cat /etc/shells |
The ‘cat’ syntax is used to read text files. The shell file within the etc. directory stores the types of shell scripting programs in a Linux, Unix, or Debian environment. Each shell has its own unique set of syntaxes and its own set of built-in functions. The DOS or Windows also have their own shell known as the infamous ‘command prompt’ or the ‘PowerShell’ from Windows 8.1 onwards, but it is still not as powerful as the bash. The cat syntax will give you the list of shells available on the system; however, to find the default shell or to check the current using shell, one can use the following syntax in the Shell Scripting Terminal:-
$ echo $SHELL |
In Shell Scripting, the ‘echo’ syntax is used to print any statement. The ‘SHELL’ variable is assigned to print the current shell in the given example. The dollar sign after ‘echo’ is used to represent the variable in the script.
Shell Scripting Series of commands
Now that you know what a shell is, let’s explore Shell Scripting and why it’s called that. Shell Scripting is nothing but a series of commands in a simple text file ending with an extension ‘.sh’. The ‘.sh’ determines that it is a shell executable file. After writing a shell script, one would need to change the executable permission using the ‘chmod function.’ n the UNIX or Linux environment, newly created shells are not executable by default unless their permissions are changed explicitly. Shell Scripting is almost similar to that Batch programming from Windows. Still, the difference is that Shell Scripting is more powerful and provides a much more high-end interactive environment and tab completions. One needs to make use of a good text editor to write a shell script. I know a few better ones are Vim, Gedit, and Leafpad.
Unlike other programming languages, Shell scripts are quicker to write and execute. You don’t need to download any specific bundles or dependencies to execute. Regular persons, not only system administrators, can execute automated scripts. The following is an example of a simple script:
#!/bin/bash echo -e “Welcome to the first shell program.\n”echo -e “Write something here and this program will print back the same.\n” #asking the user for inputread input #reading the input from userecho “You entered: $input”exit 0 |
Now let me explain all the parameters in the above code. First of all, write the above code in a text file in an ‘as is’ format and save it with an extension ending with ‘.sh.’ For example, name the file ‘main_program.sh’. Make sure that it is not ‘main_program.sh.txt.’ Most beginners tend to do the same, and then it is not executable. After copying the above code, modify its permissions in the terminal using the following syntax:-
$ chmod +x main_program.sh OR $ chmod 755 main_program.sh |
Feel free to use either of the above commands since both of them do the same thing. Modern bash shell scripting programs use ‘+x’ instead of ‘755’, so either can be used interchangeably. After you have changed the permissions, type the following to execute it.
$./main_program.sh |
You can use the period and slash to identify and execute shell scripts. Once you execute the above script, it will print the first echo statement and then will ask you to enter something. Once you enter it, it will print out the same thing back to you and exit the interpreter.
So let me explain all the syntaxes above. The ‘#!/bin/bash is used to tell the interpreter that this is a bash script and the Bash shell scripting program is located in the ‘bin’ directory. The ‘echo’ command is used to print any statement, and the ‘-e’ flag is used to indicate to the shell that there will be either a new line identified as a ‘slash n or \n’ or a tab ‘slash t or \t’ or the likes of it inside the echoed statement. The hash part on the second line is used to identify a comment in a bash script.
Whatever you write after hash gets commented out and won’t be executed. But this does not apply to the ‘#!/bin/bash’ on the first line. The script is designed to work this way, with the ‘read’ syntax used to read input from the user. And the ‘input’ I have mentioned after the ‘read’ syntax is the variable in which the input from the user is stored.
It is possible to use any variable instead of the input in the script. The second last line of the script uses the $input variable to identify itself and output the stored input. The script uses the exit command to exit the interpreter cleanly with exit code zero.
UnInteractive Scripting
Now the above example is just quite easy. Instead of providing all the inputs manually, you can use built-in modules. The following code prints out the username of the currently logged-in user, the current time, the calendar date, and the number of logins.
#!/bin/bash # This script will print the current logged in user info, the number of users logged in, the date,the time and the calendar echo “Hello, $USER”echo “Today’s date and time is `date`”echo “Number of Users logged in : `who | wc -l`”
echo “Calendar” cal exit 0 |
Execute this script after changing the permissions of the file above. Now, you might be wondering that in the previous script, we actually entered the input, and then it returned the input using the variable. But here, we have not provided any input inside the ‘$USER’ variable; then, how come it prints the exact user? As mentioned earlier, $SHELL is defined by default because these things are preconfigured in the system. Yes, similar is the case with the $USER here. As for the date, if you just type in ‘date’ without any quotes in the terminal, it will print out the exact date and time. I just used a single quote ( ` ) to identify itself as a syntax rather than a part of an echoing statement. The single quote ( ` ) will execute any syntax printed inside it, be it in a statement or anywhere else.
To check the current users logged in along with the logged-in processes and their start time, you can use the ‘who’ command. But as for the above command, I actually truncated it just to output the number of users logged in using the ‘wc -l’ command. The pipe symbol, also called the bar ( | ), combines two commands in Shell Scripting. The pipe actually stands for much more than that, but I won’t be going into much detail here. And finally, I printed the calendar using the ‘cal’ built-in command for printing out the current date in the calendar.
The Exit Zero
Exit Zero is something that I haven’t explained in any of the above-written programs. The main reason is ‘Exit Zero’ has much more gravity to it than any of the syntaxes above in Shell Scripting. By default, whenever you execute a script in a Linux or Unix distribution, it returns two types of values or, more specifically, the exit status to check whether the execution of the shell script is successful or not. If it returns the value as Zero, then the execution was successful, else it either did not get executed, or there was some error during execution. Thus, typing in ‘exit 0’ at the end of any script checks the execution status of the script.
Everything I have covered here is just the least basic part of the Shell scripting in the Linux distribution. The exit zero part and the pipe play an important role when executing multiple shell scripting in Linux commands and using it along with the conditional if and else statements.
Recommended Articles
Here are some articles that will help you to get more detail about the Shell Script Types For Linux Newbies, so just go through the link.