Bash
Declare Var
variable=10
str=Hello World # wrong
str="Hello World" # correct
str='Hello World' # correct
Single Quote: interprets every enclosed character literally
Double Quote: all characters are viewed literally except "$", "`", and "" meaning variables will be expanded in an initial substitution pass on the enclosed text
$()
: Command Substitution, set the value of the variable to the result of a command or program
``: Command Substitution, The backtick method is older and typically discouraged as there are differences in how the two methods of command substitution behave
Variable Name | Description |
---|---|
$0 | The name of the Bash Script |
9 | The first 9 arguments to the Bash Script |
$# | Number of arguments passed to the Bash Script |
$@ | All arguments passed to the Bash Script |
$? | The exit status of the most recently run process |
$$ | The process ID of the current script |
$USER | The username of the user running the script |
$HOSTNAME | The hostname of the machine |
$RANDOM | A random number |
$LINENO | The current line number in the script |
Read User Input
echo "Hello there, would you like to learn how to hack: Y/N?"
read answer
echo "Your answer was $answer"
read -p 'Username: ' username # -p allows us to specify a prompt
read -sp 'Password: ' password # -s makes the user input silent
echo "Thanks, your creds are as follows: " $username " and " $password
Conditional Statements
if [ <some test> ] # [ ] square bracket can be replaced by test
then
<perform action>
elif [ <some test> ]
then
<perform different action>
else
<perform yet another different action>
fi
Operator | Desciption: Expression True of... |
---|---|
!EXPRESSION | The EXPRESSION is false |
-n STRING | STRING length is greater than zero |
-z STRING | The length of STRING is zero (empty) |
STRING1 != STRING2 | STRING1 is not equal to STRING2 |
STRING1 = STRING2 | STRING1 is equal to STRING2 |
INTEGER1 -eq INTEGER2 | Equal |
INTEGER1 -ne INTEGER2 | Not Equal |
INTEGER1 -gt INTEGER2 | Greater Than |
INTEGER1 -lt INTEGER2 | Less Than |
INTEGER1 -ge INTEGER2 | >= |
INTEGER1 -le INTEGER2 | <= |
-d FILE | FILE exists and is a dir |
-e FILE | FILE exists |
-r FILE | FILE exists and has read permission |
-s FILE | FILE exists and it is not empty |
-w FILE | FILE exists and has write permission |
-x FILE | FILE exists and has execute permission |
Logic Operator
grep $USER /etc/passwd && echo "$USER found!" # the second half is executed iff the first half is True
grep $user2 /etc/passwd && echo "$user2 found!" || echo "$user2 not found !" # when grep does not find a matching line and returns False, the second echo command after the OR (||) operator is executed instead.
Loops
for var-name in <list>
do
<action to perform>
done
for ip in $(seq 1 10); do echo 10.11.1.$ip; done
for i in {1..10}; do echo 10.11.1.$i;done
while [ <some test> ]
d
<perform an action>
done
counter=1
while [ $counter -lt 10 ]
do
echo "10.11.1.$counter"
((counter++)) # The ((counter++)) line uses the double-parenthesis (( )) construct to perform arithmetic expansion and evaluation at the same time
done
Functions
# format 1
function func_name {
...
}
# format 2
func_name () {
...
}
# function example
print_me () {
echo "You have been printed!"
}
print_me
# function example 2
pass_arg() {
echo "Today's random number is: $1" # $1 is the passed in argument
}
pass_arg $RANDOM
Scope
By default, a variable has a global scope, meaning it can be accessed throughout the entire script.
A local variable can only be seen within the function, block of code, or subshell in which it is defined.
local name="Joe"