|
|
|
Functions let you group commands together and reuse them throughout your script. They’re essential for organizing larger scripts. |
|
|
Define a function using the |
|
|
Bash
Bash supports an alternative syntax using the
|
|
|
You can also combine both syntaxes: |
|
|
Call a function by using its name. |
|
|
Functions can accept arguments via \(1, \)2, etc. |
|
|
\(@ contains all arguments, \)# is the count. |
|
|
Functions return exit status with |
|
|
To return string values, use command substitution. |
|
|
Variables in functions are global by default. Be careful with naming to avoid conflicts. |
|
|
Bash
Bash provides the |
|
|
Functions must be defined before they’re called. Define all functions at the top of your script. |