Bash Basics

What is bash?
Bash is a command language interpreter.

What environment does bash provide?
Bash provides an environment in which users can execute commands and run applications.

What do penetration testers and security practitioners write bash scripts for?
Penetration testers and security practitioners write bash scripts for automating a wide variety of tasks.

...

Exploring the Shell

How do you check what version of bash you're using?
To check what version of bash you're using, run:

$ bash --version

Checking Environment Variables

What does bash load when you run it in a terminal?
When you run bash in a terminal, it loads a set of environment variables with every new session that gets invoked.

What uses the environment variables loaded by bash and why?
The environment variables loaded by bash are used by programs for various purposes like discovering the identity of the user running the script, the location of their home directory, their default shell, and more.

How do you list all of the environment variables set by bash?
To list the environment variables set by bash, run:

$ env

How do you list a specific environment variable set by bash?
To list a specific environment variable set by bash, run:

$ echo ${VARIABLE_NAME}

What are some of the default bash environment variables?
Some of the default bash environment variables include:

  • BASH_VERSION - The bash version running.
  • BASHPID - The process ID of the current bash process.
  • GROUPS - The list of groups that the running user is a part of.
  • HOSTNAME - The hostname of the machine.
  • OSTYPE - The type of operating system.
  • PWD - The current working directory.
  • RANDOM - A random number from 0 to 32767.
  • UID - The user ID of the current user.
  • SHELL - The full pathname to the shell.

Where can you go to see the full list of bash environment variables?
To see the full list of bash environment variables, go to https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html.

Running Linux Commands

Todo

This section talks about reading the manual for certain commands and how arguments can be written for them.

Elements of a Bash Script

...

Does Bash have an official style guide?
No, Bash doesn't have an official style guide.

What is a style guide you can adhere to? (1)
A style guide you can adhere to is Google's Shell Style Guide.

The Shebang Line

What should every script begin with?
Every script should begin with the shebang line.

What does the shebang line consist of?
The shebang line consists of a hash, an exclamation mark, and the full path to the binary of the script interpreter.

What is the usual shebang line for bash scripts?
The usual shebang line for bash script is #!/bin/bash.

What is a more portable shebang line for bash scripts?
A more portable shebang line for bash scripts is #!/usr/bin/env bash.

What can you add to the shebang line?
You can add optional arguments to the shebang line.

What are some arguments you can add to the shebang line and what they do?
Some arguments you can add to the shebang line and what they do include:

  • -x - Prints all commands and their arguments as they're executed.
  • -r - Restricts certain potentially dangerous commands.

Comments

...

Commands

...

Execution

...

Debugging

What bash flag will read the commands in the script but not execute them?
The flash flag which will read the commands in the script but won't execute them is the -n flag.

How do you set a bash flag from within a bash script?
You set a bash flag from within a bash script using the set -<flag> or set +<flag> command.

Example of using set to turn verbose mode on and off
#!/bin/bash

# Turn on verbose mode
set -x 

# Code

# Turn off verbose mode
set +x 

Basic Syntax

What do most basic bash scripts consist of?
Most basic bash scripts consist of lists of Linux commands collected in a single file.

...

When does bash advance to the next line in a script?
Bash advances to the next line in a script once the current line has finished executing.

What needs to be validated for every command that is run?
For every command that is run, whether or not the command executed correctly needs to be validated.

Variables

What are the two possible ways you can assign a value to a variable in bash?
The two possible ways you can assign a value to a variable in bash are by:

  1. Directly assigning values to variables.
  2. Executing bash commands and storing their output in a variable.

Is there a type system in bash?
No, there isn't a type system in bash.

What does bash treat every variable as?
Bash treats every variable as a character string.

Assigning and Accessing Variables

Example of assigning a value to a variable named book
book="Black Hat Bash"
Example of accessing the value of a variable named book while calling the echo command
echo "This book's name is ${book}"

Are curly braces when accessing the value of a variable optional?
Yes, curly braces when accessing the value of a variable are optional.

Example of assigning the output of the ls command to a variable named root_directory
root_directory=$(ls -ld /)

Can you put whitespace around the assignment operator (=)?
No, you can't put whitespace around the assignment operator (=).

Unassigning Variables

How do you unassign variables in bash?
To assign variables in bash, you use the unset command.

Example of unassigning a variable named book
unset book

Scoping Variables

How do you make it so a variable is only accessible within a specific block of code?
To make it so a variable is only accessible within a specific block of code, you use the local command before declaring the variable name.

Example of a local variable named name which is only accessible within a function named print_name
#!/bin/bash

print_name(){
	local name
	name="Black Hat Bash"
	echo "${name}"
}

print_name

echo "${name}"

...

Arithmetic Operators

What are the seven arithmetic operators you can use in bash?
The seven arithmetic operators you can use in bash are:

  1. + - Addition.
  2. - - Subtraction.
  3. * - Multiplication.
  4. / - Division.
  5. % - Modulo.
  6. += - Incrementing by a constant.
  7. -= - Decrementing by a constant.

What are the three ways you can use arithmetic operators in bash?
The three ways you can use arithmetic operators in bash are:

  1. Using the let command.
  2. Using the double parenthesis syntax $((expression)).
  3. Using the expr command.
Example of using the let command
let result="4 * 5"
Example of using the double parenthesis syntax $((expression))
result=$((4 * 5))
Example of using the expr command
result=$(expr 4 * 5)

What is the difference between the expr command and the other two methods of performing arithmetic in bash?
The difference between using the expr command and the other two methods of performing arithmetic in bash is that the expr command can evaluate expressions other than basic arithmetic, like calculating the length of a string.

Arrays

...