Bash Function
A function is a block of code that performs a specific task.
Instead of writing the same commands over and over, you can put them inside a function and call it whenever you need.
Think of a function as a mini-program inside your script.
How to Define a Function?
The basic syntax of a Bash function looks like this:
my_function() {
commands…
}
The name goes first, followed by (), then the body of the function inside {}.
Example:
#!/bin/bash
hello() {
echo "Hello from a function!"
}
This defines a function called hello. It doesn’t do anything until you call it.
Calling (Using) a Function
To run a function, just write its name:
#!/bin/bash
hello() {
echo "Hello from a function!"
}
hello
If you save this as func.sh and run:
bash func.sh
or
./func.sh
The Output is:
Hello from a function!
SOCIAL SHARE CARD GENERATOR