tags:
- cybersecurity
- bash
- programming
- security
- software
- book
- notes
created: 2024-12-18
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.
...
How do you check what version of bash you're using?
To check what version of bash you're using, run:
$ bash --version
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.
This section talks about reading the manual for certain commands and how arguments can be written for them.
...
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.
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.
...
...
...
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.
set
to turn verbose mode on and off#!/bin/bash
# Turn on verbose mode
set -x
# Code
# Turn off verbose mode
set +x
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.
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:
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.
book
book="Black Hat Bash"
book
while calling the echo
commandecho "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.
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 (=
).
How do you unassign variables in bash?
To assign variables in bash, you use the unset
command.
book
unset book
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.
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}"
...
What are the seven arithmetic operators you can use in bash?
The seven arithmetic operators you can use in bash are:
+
- Addition.-
- Subtraction.*
- Multiplication./
- Division.%
- Modulo.+=
- Incrementing by a constant.-=
- 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:
- Using the
let
command.- Using the double parenthesis syntax
$((expression))
.- Using the
expr
command.
let
commandlet result="4 * 5"
$((expression))
result=$((4 * 5))
expr
commandresult=$(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.
...