bash function how to


Generally function are great for reuse block of code. Define a function can be done in two ways. First option:

function_name () {

Second option use keyword "function":

function function_name {

The syntax of first option is similar to C function. Let's create a new function which will print greeting to user:

hello() {
echo "Hello" `whoami`.

To call the function "hello" in your script just write: hello.

Function can be defined in single line:

foo() { echo "Function foo call..."; }

It is important to put spaces after { and write ; (semicolon) after echo command.

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.