> For the complete documentation index, see [llms.txt](https://docs.h4rithd.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.h4rithd.com/languages/bash.md).

# Bash

## /bin/bash

```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

```bash
#!/bin/bash
```

### Arrays

```bash
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

```bash
-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

```bash
&&	logical AND (Ex: [[ -e "$1" && -r "$1" ]])
!	logical negotation NOT
||	logical OR
```

### Arithmetic Operators

```bash
+	        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

```bash
read -p "Select your option: " option
echo $option

-p    Input remains on the same line
```

* Output

```bash
..... | tee -a filename.txt

-a / --append
```

## 02. Flow Control

### Loops

* For Loops

```bash
## ------------------| 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

```bash
## A statement is executed as long as a condition is fulfilled (true).
while [ <CONDITION> ]
do
	<TASK>
done
```

* Until Loops

```bash
## 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

```bash
if [ <CONDITION> ]
then
	<TASK-I>
elif [ CONDITION ]
then
	<TASK-II>
else
	<TASK-FINAL>
fi
```

### Case Statements

```bash
case <expression> in
	pattern_1 ) statements ;;
	pattern_2 ) statements ;;
	"*") exit 0 ;;
esac
```

## Functions

```bash
## ------------------| Method 1
function name {
	<commands>
}

## ------------------| Method 2
name() {
	<commands>
}
```

* Parameter Passing

```bash
function print {
	echo $1 $2 $3
}

two="Second"

print_pars "First" "$two" "3"
```

* Return Values (Return Code)

```bash
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
```
