13Languages
Bash
/bin/bash
bash9 lines
## ------------------| Environment Variablesenv # List all envsecho $PATH # List path envecho $USER # Current userecho $HOME # Current user's homeecho $PWD # Current workind direcho $HISTSIZE # Number of commnds (in memory/current session)echo $HISTFILESIZE # How many commands in ~./historyexport ip="0.0.0.0" # Make ip as global variable01 Introduction
Shebang
bash1 line
#!/bin/bashArrays
bash2 lines
array=(hello this is an array 123)echo ${array[2]}Arguments
text9 lines
## ------------------| 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
text6 lines
== 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
text6 lines
-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
bash11 lines
-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
bash3 lines
&& logical AND (Ex: [[ -e "$1" && -r "$1" ]])! logical negotation NOT|| logical ORArithmetic Operators
bash8 lines
+ Addition- Substraction* Multiplication/ Division% Modulusvariable++ Increase the value of the variable by 1variable-- Decrease the value of the variable by 1${#variable} Length of the variableInput / Output
- Inputs
bash4 lines
read -p "Select your option: " optionecho $option
-p Input remains on the same line- Output
bash3 lines
..... | tee -a filename.txt
-a / --append02 Flow Control
Loops
- For Loops
bash14 lines
## ------------------| With Numbersfor $i in 1 2 3 4do echo $idone
## ------------------| With Filesfor $words in file1 file2 file3do echo $wordsdone
## ------------------| One linefor i in {1..5};do echo $i;done- While Loops
bash5 lines
## A statement is executed as long as a condition is fulfilled (true).while [ <CONDITION> ]do <TASK>done- Until Loops
bash8 lines
## The code inside a until loop is executed as long as the particular condition is false.counter=0until [ $counter -eq 10 ]do # Increase $counter by 1 ((counter++)) echo "Counter: $counter"doneIf Else
bash9 lines
if [ <CONDITION> ]then <TASK-I>elif [ CONDITION ]then <TASK-II>else <TASK-FINAL>fiCase Statements
bash5 lines
case <expression> in pattern_1 ) statements ;; pattern_2 ) statements ;; "*") exit 0 ;;esacFunctions
bash9 lines
## ------------------| Method 1function name { <commands>}
## ------------------| Method 2name() { <commands>}- Parameter Passing
bash7 lines
function print { echo $1 $2 $3}
two="Second"
print_pars "First" "$two" "3"- Return Values (Return Code)
bash8 lines
1 General errors2 Misuse of shell builtins126 Command invoked cannot execute127 Command not found128 Invalid argument to exit128+n Fatal error signal "n"130 Script terminated by Control-C255\* Exit status out of range