Summary
The Introduction to Bash Scripting module is designed for cybersecurity detection engineers seeking to enhance their scripting skills and Security professionals looking to automate and streamline threat detection processes. Throughout this module, students will develop proficiency in Bash scripting to streamline cybersecurity detection tasks and enhanced abilities in automating security processes and incident response.
Recommended prior knowledge:
- Basic knowledge of Linux/Unix operating systems
Introduction to Bash
History Of Bash
The Unix shell serves as an interface that allows users to interact with operating systems. This shell receives the user's commands and passes those commands to the operating system. Shell is both a scripting language and a program capable of interpreting commands.
The first Unix shell was developed by Stephen R. Bourne, one of the original creators of Unix, in 1979, and was therefore named Bourne Shell. The Bourne shell (sh) became the most widely used shell on Unix and Unix-like systems.
In the mid-1980s, Brian Fox developed Bash (Bourne Again Shell) for the GNU project with the aim of replacing Bourne Shell. Bash is a shell that encompasses the features of Bourne Shell but also provides more advanced features and a user-friendly interface. Bash is used as the standard shell for many Unix-like systems, including Linux and macOS.
The development of Bash continued with the fixing of various bugs as well as adding new features such as command line editing, shell job control, shell functions, and many more.
We can say that Bash is the tool of choice by many system administrators and developers, as it proves the importance and flexibility of the shell and also makes it easy to write scripts. This has made Bash frequently used, especially in system administration and automated tasks.
Cons and Pros
Because Bash was created in the UNIX systems, it has the inherent robustness and problem-solving abilities of that environment. Let's take a look at the strengths that characterize Bash:
Works Anywhere
You can find Bash almost anywhere. It works on Linux, Mac, and even Windows (thanks to the Windows Subsystem for Linux). In fact, there is a BASH in your car and on your TV at home, even if you don't see it. This means that the scripts you write will work on almost any platform.
Automation Capabilities
Bash is the number one method to automate tasks. It is a powerful tool which you can get repetitive tasks done. Using Bash's capabilities is a great way to save time and reduce errors.
Productivity
Thanks to Bash, you have the ability to combine complex commands and perform many tasks at once. This helps you to be more productive.
Integration Capability with Many Different Tools
Bash integrates with many Unix commands and tools perfectly. This allows you to perform more complex operations and use many different tools in the same script.
We've seen Bash's strengths above. Unfortunately, it also has some weaknesses. Before listing them, let's point out that Bash is actually excellent as a system management tool, its disadvantages appear when compared to other modern scripting languages:
Complexity
Bash can be a bit complex, especially for beginners. Its syntax can be a bit odd and difficult to remember from time to time.
Debugging: Debugging Bash can be a bit of a hassle. Bash usually does not display the line number with error, and so this makes it difficult to find errors.
Performance
Bash is a bit slower in terms of performance compared to other languages. While this is not usually a problem, it may be from time to time when working with very large datasets or when you need to perform tasks very quickly.
Advanced Features
Bash does not have some advanced features that languages like Python or JavaScript offer. For instance, Bash lacks features like first-class functions, data structures, etc.
To summarize, Bash is a great option in many cases, especially for tasks related to system administration and automation. However, other languages may be more suitable for certain situations and requirements.
Terminology
In this section, let's explain some very basic concepts for those unfamiliar with the Unix world. These will make the later stages of the module more understandable.
Shell
Shell allows users to communicate with the operating system. Shell is a type of user interface and is usually text-based. Shells also feature a scripting language and allow users to save commands as a written script and run this script automatically later. Bash is one of the most popular Unix shells and often comes as the default shell on Linux and macOS.
Command Line (CLI)
The command line is an interface where users enter commands into the shell. It is usually accessed via a terminal or console application. Users tell the operating system what to do by typing text-based commands and pressing “Enter”.
Command
A command is an instruction given to the operating system to perform a certain action. For example, ls is a Unix command that lists files and directories in the current directory to the user. cd is another command and it changes the current working directory.
Script
A script is an automated set of commands. These commands are stored in a file and then interpreted and executed by the shell. Bash scripts are usually stored in files ending with the .sh extension and can contain a number of command and control structures (if-else constructs, loops, etc.).
Pipelining
Pipelining is the ability to use the output of one command as the input of another command. This is done with the | symbol. For example, the ls | grep .txt command passes the output of the ls command to the grep command and lists only files with the .txt extension.
Environment Variables
Environment variables allow the shell and the programs it runs to store and access certain values. For example, the PATH environment variable specifies which directories the shell will check for commands. These environment variables can be set system-wide or for a specific user.
STDIN, STDOUT ve STDERR
These terms refer to standard channels for the input and output of the shell and programs. STDIN usually represents input from the keyboard, STDOUT represents correct output and STDERR represents erroneous output.
Execution Permissions
On Unix-based systems, it determines whether a file or script is executable. These permissions can be changed with the chmod command.
These terms are some of the basic concepts you will encounter in the Bash and general Unix world. Understanding them is an essential part of working effectively on a Unix-based system and writing Bash scripts.
Hello World
Let's write a little bash script together:
First, open a text editor. On Linux it can usually be nano, vi or emacs. On Windows you can use Notepad or another text editor. Basically, what we're going to do is create a text file with .sh extension
Type the following two lines in the text editor:
#!/bin/bash
echo "Hello World"
The #!/bin/bash line indicates that this script should be run in Bash.
The echo command, on the other hand, prints text to the screen.
Save this file as hello_word.sh .
Now we have to make sure the script is executable. We can do this by running the following command in terminal:
chmod +x hello_word.sh
This command adds executable permissions to hello_world.sh.
Finally, we can run our script:
./hello_word.sh
What do you see on the screen?
We've made a basic example of creating and running a simple "Hello Word" script in Bash. This script is a great starting point for demonstrating the simplicity and ease of use of the Bash scripting language.