Bash Scripting Flashcards
What is GNU Bourne-Again Shell (Bash)?
It’s a powerful work environment and scripting engine. A competent security professional skillfully leverages Bash scripting to streamline and automate many Linux tasks and procedures.
How to create a simple ‘Hello World’ Bash script?
#!/bin/bash # Hello World Bash Script echo "Hello World!"
How to declare a variable “first_name” as “Kacper”.
kali@kali:~$ first_name=Kacper
How to display “first_name” variable using Bash?
kali@kali:~$ echo $first_name
How to use of command substitution (whoami) and variables?
user=$(whoami)
user=whoami
How to instruct bash to print additional debug output, so we could more easily see the commands that were executed and their results.
!/bin/bash -x
Describe special Bash variable ‘$0’
The name of the Bash script
Describe special Bash variable ‘$1 - $9’
The first 9 arguments to the Bash script
Describe special Bash variable ‘$#’
Number of arguments passed to the Bash script
Describe special Bash variable ‘$@’
All arguments passed to the Bash script
Describe special Bash variable ‘$?’
The exit status of the most recently run process
Describe special Bash variable ‘$$’
The exit status of the most recently run process
Describe special Bash variable ‘$USER’
The username of the user running the script
Describe special Bash variable ‘$HOSTNAME’
The hostname of the machine.
Describe special Bash variable ‘$RANDOM’
A random number.
Describe special Bash variable ‘$LINENO’
The current line number in the script
How to collect user input using read “answer”?
!/bin/bash
echo “Hello there, would you like to learn how to hack: Y/N?”
read answer
echo “Your answer was $answer”
What ‘-p’ option do in read command?
Allows to specify a prompt.
What ‘-s’ option do in read command?
Makes the user input silent. The latter is ideal for capturing user credentials.
How to prompt user for input and silently read it using read?
#!/bin/bash # Prompt the user for credentials read -p 'Username: ' username read -sp 'Password: ' password echo "Thanks, your creds are as follows: " $username " and " $password
What is ‘#!’ in bash?
Shebang
What is ‘/bin/bash’ in Bash scripting file?
Absolute path.
What char is used to create comments in bash scripting?
#
How to print string “Hello World!” to terminal?
echo “Hello World!”
How to make script executable?
chmod +x
How to run script “hello-world.sh”?
./hello-world.sh
General syntax for the if statement in bash scripting?
if [ some test ]
then
perform an action
fi
How to use the if statement in Bash to check age, and decline if age is less than 16?
#!/bin/bash # if statement example read -p "What is your age: " age if [ $age -lt 16 ] then echo "DECLINE" fi
What is option ‘-lt’ in bash scripting?
Less than.
Describe operator !EXPRESSION
The EXPRESSION is false
Describe operator -n STRING
STRING length is greater than zero