Function



Define a function


• 2 ways to define a function

# First way
spaced() {
    # parameters are not named; they are positional
    echo
    echo "######################"
    echo "$1"                       # 1st arg passed into the func
    echo "######################"
    echo
}


# Second way
# Recommended, more readable
function javatest() {
    if [[ $number -eq 99 ]]; then
        spaced "You win! You're amaaaaazziinnnggg!"     # function call; no ()
    elif (( $number < 10 )); then
        spaced "You're a courageous one"
        
        # set a variable interactively
        echo "Hi ${name}, to avert a horrible death, please enter the password"
        read password
        
        if [[ "$password" !"Java" ]]; then
            spaced "Well, at least you're not a Java Programming sympathizer. \
            You can go now."

        else
            spaced "DIIEEEE! Actually, nevermind. Writing Java is enough \
            of a hellish punishment. You are free to leave"

        fi
    fi
}


Call a Function



javatest $number
exit 0

Index