Linux for Developers
/Advanced
Functions
Definition
Reusable blocks of code within a Bash script. They allow you to group complex commands under a single name, keeping scripts organized and avoiding copy-pasting the same code.
Explain Like I'm New
A mini-script inside your script. You teach the script how to 'CheckDatabase()'. Then, whenever you need to check the database, you just say 'CheckDatabase()' instead of typing out the 20 lines of code again.
Real World Example
A backup script has a function `log_message() { echo "[$(date)] $1" >> backup.log; }`. Throughout the 500-line script, the developer can just type `log_message "Starting backup"` to instantly write neatly formatted timestamps to the log file.
Common Use Cases
- •Modular scripting
- •Dry (Don't Repeat Yourself) programming
Interview Questions
basic
- Do Bash functions require you to define what type of data they return (like integer or string)?
intermediate
- How do you pass a variable or argument into a Bash function?