Executing mail command from inside a function causes a "fork bomb"

You're invoking the function mail from within the same function:

#!/bin/bash

mail() {
    # This actually calls the "mail" function
    # and not the "mail" executable
    echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "[email protected]"
}


mail

exit 0

This should work:

#!/bin/bash

mailfunc() {
    echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "[email protected]"
}

mailfunc

exit 0

Note that function name is no longer invoked from within the function itself.


Otherwise:

mail(){

    echo olly olly oxenfree | command mail -s 'and the rest' and@more
}

...should work fine.


The most "traditional" solution in these cases is actually to call the command with full path:

mail() {
    echo "Free of oxens" | /usr/bin/mail -s "Do you want to play chicken with the void?" "[email protected]"
}

All other answers work, and are probably more portable, but I think that this is the most likely solution you'd find in scripts in the wild real world, so I'm including it for completeness.