# Bash scripting

{% embed url="<https://devhints.io/bash>" %}
Bash scripting cheatsheet
{% endembed %}

## Bash it

### <sub>Bash is Shell and it’s NOT Batch and</sub> <sub></sub><sub>`.bat`</sub> <sub></sub><sub>is written in Batch and for windows only;</sub> <sub></sub><sub>`.sh`</sub> <sub></sub><sub>is written in Bash and for Unix-like systems</sub>

The first line:

```bash
#!/bin/bash

echo "text here" #just like printf(); in C
```

Give the script file executable permissions

`Chmod +x yourfile.sh` and run it using `./script.sh`

It works like in the terminal but it’s in a script so you don’t have to type it separately

```bash
#!/bin/bash

echo "hello"
ls
whoami
id
```

## Variables

Like many languages you give the value to the variable

add a `$` onto front of our variable name

```bash
#!/bin/bash
$ name="Vor Torx"
$ echo $name
(returns Vor Torx)
```

### Debugging

`bash -x ./file.sh`

This tells you which lines are working and which lines are not. If you want to debug at a certain point you can insert set -x into your script and set +x to end the section like the following

The outputs will add `+` if it’s executed successfully and if there’s an error it will add `-` on the line

## Parameters

A Parameter is a variable so we still need to add `$` for the variable

```bash
#!/bin/bash
name=$1
echo "You are $name"
```

If you want to add a second argument we can just add a `$2` instead of `$1`

It’s like this: `./script.sh John Amy` and it will return Amy

```bash
#!/bin/bash
echo Enter your name:
read name
name=$1
echo "Hello $name!"
```

`$#` -number of arguments

`$0` -file name in the current script

## Arrays

Like `char arr[n];` in C; commonly uses `var=(index_position)`

`var=('man' 'bug' 'not' 'fixed')` and the stored values start at 0 just like C, so the index of `man` is 0, `bug` is 1 and so on

Remember to use **single quote** when the element **contain spaces** otherwise **it’s not required**

We can use `echo` and output the values `echo "${transport[@]}"` `@` means all arguments

### Change elements

We can change/delete the elements using `unset`

```bash
#!/bin/bash
real=('man' 'bug' 'not' 'fixed') 
echo "${real[@]}" #echo out all the elements

unset real[0] #change the element
real[0]='Josh' #you can use read if you want to insert a element you like
echo "${real[@]}"
```

Outputs:

```bash
man bug not fixed #original elements in 'real' array
Josh bug not fixed #changed elements
```

## Conditionals

The conditions are met just like using `if(){}` statement in C

The if statement in Bash looks like:

```bash
if [argument(s)]
then
	something
else
	something else
fi
#!/bin/bash
num=(20 30)
if [ "${num[0]}" -gt "${num[1]}" ]
then
	echo "${num[0]} is bigger"
else
	echo "${num[1]} is bigger"
fi
```

| **Operator** | **Description**                                                                                                                          |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
| -eq (=)      | Checks if the value of two operands are equal or not; if yes, then the condition becomes true.                                           |
| -ne (≠)      | Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.                          |
| -gt (>)      | Checks if the value of left operand is **greater than** the value of right operand; if yes, then the condition becomes true.             |
| -lt (<)      | Checks if the value of left operand is **less than** the value of right operand; if yes, then the condition becomes true.                |
| -ge (≥)      | Checks if the value of left operand is **greater than or equal** to the value of right operand; if yes, then the condition becomes true. |

```bash
#!/bin/bash
filename=$1 #assigns the value of the first command-line argument
if [-f "$filename"] && [-w "$filename"]
then
		echo "Heyy" > $filename
else
	touch "$filename" #make a file with e file name you gave
	echo "Heyy" > $filename #write Heyy in the file
fi
```

`./script.sh Yoo.txt GG.txt` it will take the `Yoo.txt` cuz you use `filename=$1` and $1=Yoo.txt

`cat Yoo.txt` → Heyy

| `[[ -e FILE ]]`         | Exists                  |
| ----------------------- | ----------------------- |
| `[[ -r FILE ]]`         | Readable                |
| `[[ -h FILE ]]`         | Symlink                 |
| `[[ -d FILE ]]`         | Directory               |
| `[[ -w FILE ]]`         | Writable                |
| `[[ -s FILE ]]`         | Size is > 0 bytes       |
| `[[ -f FILE ]]`         | File                    |
| `[[ -x FILE ]]`         | Executable              |
| `[[ FILE1 -nt FILE2 ]]` | 1 is more recent than 2 |
| `[[ FILE1 -ot FILE2 ]]` | 2 is more recent than 1 |
| `[[ FILE1 -ef FILE2 ]]` | Same files              |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vix-w1zzer.gitbook.io/vixwizzer/notes/linux/bash-scripting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
