Bash
/bin/bash
## ------------------| Environment Variables
env # List all envs
echo $PATH # List path env
echo $USER # Current user
echo $HOME # Current user's home
echo $PWD # Current workind dir
echo $HISTSIZE # Number of commnds (in memory/current session)
echo $HISTFILESIZE # How many commands in ~./history
export ip="0.0.0.0" # Make ip as global variable
01. Introduction
Shebang
#!/bin/bash
Arrays
array=(hello this is an array 123)
echo ${array[2]}
Arguments
## ------------------| Maximam 9 ($0-$9)
./script.sh ARG1 ARG2 ARG3 ... ARG9
$0 $1 $2 $3 ... $9
## ------------------| Special Variables / Internal Field Separator (IFS)
$# Number of arguments passed to the script.
$@ Retrieve the list of command-line arguments.
$$ The process ID of the currently executing process.
$? The exit status of the script. ( 0 <= SUCC , 1 <= FAIL).
Comparison Operators
String Operators
== is equal to [EX: "$1" == "HackTheBox"]
!= is not equal to
< is less than in ASCII alphabetical order & works only within the double square brackets [[ <condition> ]]
> is greater than in ASCII alphabetical order & works only within the double square brackets [[ <condition> ]]
-z if the string is empty (null) (EX: [[ -z $1 ]])
-n if the string is not null
Integer Operators
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to
-gt is greater than
-ge is greater than or equal to
File Operators
-e file exist ?
-f a file ?
-d a directory ?
-L a symbolic link ?
-O current user owns the file ?
-r the file has read permission ?
-w the file has write permission ?
-x the file has execute permission ?
-s the file has a size greater than 0 ?
-G the file’s group id matches the current user’s ?
-N checks if the file was modified after it was last read
Logical Operators
&& logical AND (Ex: [[ -e "$1" && -r "$1" ]])
! logical negotation NOT
|| logical OR
Arithmetic Operators
+ Addition
- Substraction
* Multiplication
/ Division
% Modulus
variable++ Increase the value of the variable by 1
variable-- Decrease the value of the variable by 1
${#variable} Length of the variable
Input / Output
Inputs
read -p "Select your option: " option
echo $option
-p Input remains on the same line
Output
..... | tee -a filename.txt
-a / --append
02. Flow Control
Loops
For Loops
## ------------------| With Numbers
for $i in 1 2 3 4
do
echo $i
done
## ------------------| With Files
for $words in file1 file2 file3
do
echo $words
done
## ------------------| One line
for i in {1..5};do echo $i;done
While Loops
## A statement is executed as long as a condition is fulfilled (true).
while [ <CONDITION> ]
do
<TASK>
done
Until Loops
## The code inside a until loop is executed as long as the particular condition is false.
counter=0
until [ $counter -eq 10 ]
do
# Increase $counter by 1
((counter++))
echo "Counter: $counter"
done
If Else
if [ <CONDITION> ]
then
<TASK-I>
elif [ CONDITION ]
then
<TASK-II>
else
<TASK-FINAL>
fi
Case Statements
case <expression> in
pattern_1 ) statements ;;
pattern_2 ) statements ;;
"*") exit 0 ;;
esac
Functions
## ------------------| Method 1
function name {
<commands>
}
## ------------------| Method 2
name() {
<commands>
}
Parameter Passing
function print {
echo $1 $2 $3
}
two="Second"
print_pars "First" "$two" "3"
Return Values (Return Code)
1 General errors
2 Misuse of shell builtins
126 Command invoked cannot execute
127 Command not found
128 Invalid argument to exit
128+n Fatal error signal "n"
130 Script terminated by Control-C
255\* Exit status out of range
Last updated