Anchor(Structural_Constructs)

Structural Constructs (stub)

Feel free to complete this section.

Anchor(Compound_Commands)

Compound Commands (stub)

Feel free to complete this section.

Anchor(Subshells)

Subshells (stub)

Feel free to complete this section.

Anchor(Functions)

Functions

Functions are very nifty in bash scripts. They are effectively no different than normal lines of code you might write, except they only get called when you decide to call it. Take this for example:

    $ sum() {
    >   echo $1 + $2 = $(($1 + $2))
    > }

This will do absolutely nothing when run. This is because it has only been stored in memory, much like a variable, but it has never been called. To run the function, you would do this:

    $ sum 1 4
    1 + 4 = 5

Amazing! We now have a basic calculator, and potentially a more economic replacement for a five year-old.

A note on scope: if you choose to embed functions within script files, as many will find more convenient, then you need to understand that the parameters you pass to the script are not necessarily the parameters that are passed to the function. To wrap this function inside a script, we would write a file contain this:

sum() {
        echo $1 + $2 = $(($1 + $2))
}
sum $1 $2

As you can see, we passed the script's two parameters to the function within, but we could have passed anything we wanted (though, doing so in this situation would only confuse users trying to use the script).


Anchor(Aliases)

Aliases (stub)

Feel free to complete this section.