Bash Scripting Basics

Mayowa Fadairo
4 min readOct 3, 2021

I should probably start by saying I started learning about bash scripting a month before this article was written so forgive my mistakes and drop comments to help me get better.

A Linux server carries out a lot of activities behind the scenes to keep applications running, imagine managing several applications and keeping up with the state of each server or being able to intercept issues before they happen manually. This is close to impossible, which is one of the reasons a working knowledge of bash scripting is a valuable tool in your arsenal when dealing with servers.

A Bash script is a plain file that contains a series of commands. Commands like creating a file with touch or getting the server stats with top hence before getting into bash scripting it's important to understand some of these basic Linux commands and the Linux command line.

A common use of bash script is to automate activities such as monitoring system logs, with the script an administrator gives instructions to the server to carry out commands sequentially and this script can be set to run at a particular time/interval.

To interpret these commands on the script Linux uses a command-line interpreter known as a shell. The shell is both an interactive command language and a scripting language and is used by the operating system to control the execution of the system using shell scripts.[1]

There are several shells (e.g Bourne Shell, C-Shell, BASH )that can be installed and used on a Linux system however most distributions come with BASH as a default shell.

The name “BASH” stands for Bourne Again SHell, it was first released in the year 1989 as an open-source version of the Bourne Shell. It has been used as the default login shell for most Linux distributions and Apple macOS. A version was recently made available for windows 10[2].

Dissecting a Simple Bash Script to understand come of its core features

The gist above is a typical example of a Bash Script whose aim is to save system stats like disk usage, running processes, etc to a file and send the content of that file to an email address using sSMTP.

On line one we are introduced to the concept of the SheBang(#!). When a text file with a shebang is about to be executed the system uses the path passed after the SheBang to decide which shell is to be used to process the script. This is especially useful when more than one shell is installed on the system. In our case, the system uses the BASH to interpret this file.

On line two we see how a bash script identifies comments with the hash (#) sign. The interpreter totally ignores everything that has a hash (#) sign written in front of it.

Bash Scripts uses variables to save information that should be reused within the script. A variable can be defined with the variable name followed by the equal sign (=) then the value of the variable, with no whitespace between them as seen on line 4. Variables can be referenced with the use of a dollar sign ($) as a prefix to their name(line 5).

Any command that can be used in the Linux command line can be used in a script (it's usually a safe practice to test commands on the command line first before using them in a script). This also includes the idea of command chaining as seen on line 10, echo is used to print the text into the file created on line 4 using the >> the output operator.

On line 14 Linux command dfwhich stands for “disk filesystem” is used to display the disk space used in the file system. It defines the number of blocks used, the number of blocks available, and the directory where the file system is mounted.[4] Using the output operator >> these stats are appended into the file.

On line 18 Linux top command is used to show the Linux processes in batch mode as indicated by the -b option provided. The command is restricted to monitor only processes with process id 678 and just one iteration as specified by -p 678 n1 and the result is appended into the file.

On line 21 ssmtp is used to send the content of the file to the specified email. On the final line, the message is sent to the user that the email has been sent to the mail.

To run a bash script the command ./bashscriptfilename.sh in the directory where the script was saved. By default linux files are not executable, hence at first run an error will be thrown unless the file is made executable by chmod +x bashscriptfilename.sh

The example provided is a simple bash script that's easy to follow to grasp some of the core concepts. However, there is so much more that can be done within a script such as

  1. The use of Functions to reuse code within the script. Functions in Bash Scripts are similar to functions in a programming language in terms of use and variable scope.
  2. Conditional Statements using the if statement
  3. Loops while loops and for loops
  4. Input
  5. User Interface etc…

In conclusion, Bash scripting is a very powerful tool that can make server administration easier and efficient. I found this tutorial bash scripting tutorial very comprehensive and easy to follow, you are welcome to check it out to learn more.

Thanks for reading!

References

  1. https://en.wikipedia.org/wiki/Unix_shell
  2. https://en.wikipedia.org/wiki/Bash_(Unix_shell)
  3. https://ryanstutorials.net/bash-scripting-tutorial/
  4. https://www.javatpoint.com/linux-df#:~:text=Linux%20df%20command%20is%20used,the%20file%20system%20is%20mounted.

--

--