bash function how to

Answer

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

function_name () {
commands...
}

Second option use keyword "function":

function function_name {
commands...
}

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.