add entry on functions in bash

This commit is contained in:
thomasabishop 2023-03-05 07:49:12 +00:00
parent 5a45910265
commit 313278df30

View file

@ -0,0 +1,26 @@
---
categories:
- Programming Languages
tags:
- shell
---
# Functions in Bash
- We don't name function parameters in the function declaration. Instead we have an implied index of arguments: `$1, $2, $3,...`. When the function is called, the first value after the function name becomes `$1` by default.
```bash
function expandRange() {
declare -a expandedRange=()
for (( i=$1; i<=$2; i++ )); do
expandedRange+=($i)
done
echo "${expandedRange[@]}"
}
```
```bash
expandedRange=$(expandRange 1 4)
echo $expandedRange
# 1 2 3 4
```