Functions
Define with name() { }. Arguments are $1, $2 and $@. return sets an exit code and echo gives output.
Example
greet() { echo "Hello $1"; }
add() { echo $(( $1 + $2 )); }
greet Ada
sum=$(add 2 3)
echo "$sum"Result
Hello Ada 5
Exercise
Define a function.
hi { echo hi; }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.