add entry on functions in bash
This commit is contained in:
parent
5a45910265
commit
313278df30
1 changed files with 26 additions and 0 deletions
26
Programming_Languages/Shell/Functions_in_Bash.md
Normal file
26
Programming_Languages/Shell/Functions_in_Bash.md
Normal 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
|
||||||
|
```
|
Loading…
Add table
Reference in a new issue