Differences between revisions 1 and 2
Revision 1 as of 2008-05-14 13:58:17
Size: 1708
Editor: Lhunath
Revision 2 as of 2008-11-22 14:09:20
Size: 1708
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
[[Anchor(Structural_Constructs)]] <<Anchor(Structural_Constructs)>>
Line 9: Line 9:
[[Anchor(Compound_Commands)]] <<Anchor(Compound_Commands)>>
Line 16: Line 16:
[[Anchor(Subshells)]] <<Anchor(Subshells)>>
Line 23: Line 23:
[[Anchor(Functions)]] <<Anchor(Functions)>>
Line 55: Line 55:
[[Anchor(Aliases)]] <<Anchor(Aliases)>>

Structural Constructs (stub)

Feel free to complete this section.

Compound Commands (stub)

Feel free to complete this section.

Subshells (stub)

Feel free to complete this section.


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).

Aliases (stub)

Feel free to complete this section.

BashGuide/CompoundCommands (last edited 2013-04-13 03:29:17 by kirtland)