functions
function definition
function foo {
echo "this is a simple function"
}
function definition
foo() {
echo "this is a simple function"
}
compact function
foo() { echo "this is a compact function"; echo; }
exclusively defined function
if [ $(id -u) = 0 ]; then
root_exclusive() { echo "this is an exclusive function (root only)"; }
fi
conditionally defined function
[ $(id -u) = 0 ] && amiroot() { echo "i am root"; } || amiroot() { echo "i am not root"; }
function with explicit return status
foo() { return 0; }
function with implicit return status ($? of last command)
foo() { echo "this will return 0 as well"; }
function with result
foo() { echo "result"; }
result=$(foo)
function with arguments (starting with 1)
foo() {
local arg1="$1"
echo "first argument: $arg1"
echo "other arguments: $2 $3 ..."
}
check if arg is passed
foo() {
[ -n "$1" ] || return
}
function variables
foo() {
echo "number of function args: $#"
echo "args (as single string): $*"
echo "args (as separate strings): $@"
}
last updated: Sat Aug 12 14:29:24 2023