> For the complete documentation index, see [llms.txt](https://devops-3.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devops-3.gitbook.io/devops/docs/module-3-bash-scripting/functions.md).

# Functions

Functions are a great way to reuse code.

A Bash function is essentially a set of commands that can be called numerous times.&#x20;

The purpose of a function is to help you make your bash scripts more readable and to avoid writing the same code repeatedly.<br>

```bash
function function_name() {
 
your_commands 
}
```

Example:

```bash
#!/bin/bash 

function hello() { 

echo "Hello World Function!" 
} 

hello
```

### Website Hosting Script

```bash
#!/bin/bash
# Install nginx if not already installed
if ! command -v nginx &> /dev/null
then
    echo "nginx not found. Installing..."
    sudo apt-get update
    sudo apt-get install nginx -y
fi
# Copy your website files to the nginx document root
sudo cp -r /path/to/your/website/* /var/www/html/
# Start nginx if not already running
if ! pgrep -x "nginx" > /dev/null
then
    sudo service nginx start
    echo "nginx started"
else
    echo "nginx is already running"
fi

```

```bash
# Give Executable permission
chmod +x host_website.sh
# Run Script
./host_website.sh
```
