congdong007

Penetration Test、Software Developer

0%

Bash Scripting 01 - Intro to Bash Scripting

The GNU Bourne-Again Shell (Bash) is a powerful work environment and scripting engine. A
competent security professional skillfully leverages Bash scripting to streamline and automate
many Linux tasks and procedures. In this module, we will introduce Bash scripting and explore
several practical scenarios.

A Bash script is a plain-text file that contains a series of commands that are executed as if they had
been typed at a terminal prompt. Generally speaking, Bash scripts have an optional extension of
.sh (for ease of identification), begin with #!/bin/bash and must have executable permissions set
before they can be executed. Let’s begin with a simple “Hello World” Bash script, file named hello-world.sh:

1
2
3
#!/bin/bash
# Hello World Bash Script
echo "Hello World!"

This script has several components worth explaining:
• Line 1: #! is commonly known as the shebang, and is ignored by the Bash interpreter. The
second part, /bin/bash, is the absolute path to the interpreter, which is used to run the
script. This is what makes this a “Bash script” as opposed to another type of shell script, like
a “C Shell script”, for example.
• Line 2: # is used to add a comment, so all text that follows it is ignored.
• Line 3: echo “Hello World!” uses the echo Linux command utility to print a given string to the
terminal, which in this case is “Hello World!”.

Next, let’s make the script executable and run it:

1
2
3
kali@kali:~$ chmod +x hello-world.sh
kali@kali:~$ ./hello-world.sh
Hello World!

The chmod command, along with the +x option is used to make the script executable.
./helloworld.sh is used to actually run it.
The ./ notation may seem confusing but this is simply a path
notation indicating that this script is in the current directory. Whenever we type a command, Bash
tries to find it in a series of directories stored in a variable called PATH. Since our home directory
is not included in that variable, we must use the relative path to our Bash script in order for Bash
to “find it” and run it.

Now that we have created our first Bash script,next we will explore Bash in a bit more detail.