# ๐Ÿš Bash Function Syntax & Examples Bash functions allow you to group reusable logic in shell scripts. Below are clear examples and explanations to help you understand how to define and use them effectively. --- ## ๐Ÿ”ง Defining a Function You can define a Bash function in two main ways: ### Option 1: Classic Syntax ```bash function_name () { # commands } ``` ### Option 2: With the `function` Keyword ```bash function function_name () { # commands } ``` > ๐Ÿ’ก Both syntaxes are valid. The use of the `function` keyword is optional and mostly stylistic. --- ## ๐Ÿ“ฅ Passing Arguments Bash functions accept arguments just like scripts. These are accessed via `$1`, `$2`, etc. ### Example: Single Argument ```bash func1() { echo "Arg: $1" } func1 "Test" ``` **Output:** ``` Arg: Test ``` --- ## โž• Multiple Arguments & Calculation You can perform operations using passed arguments, like arithmetic: ```bash summery() { local sum=$(( $1 + $2 )) echo $sum } summery 5 7 ``` **Output:** ``` 12 ``` > ๐Ÿ“Œ `local` ensures `sum` is scoped within the function. --- ## โœ… Best Practices * Use `local` to avoid polluting the global scope. * Quote variables when dealing with strings. * Use descriptive names for clarity. --- ## ๐Ÿงช Try It Yourself Add these examples to a `.sh` file and run it with: ```bash bash script.sh ```