Introduction
Functions help you keep your code neat and easy to use again without rewriting it.
function_name() {
commands
}
# To run the function:
function_namegreet() {
echo "Hello, friend!"
}
greetadd() {
echo $(( $1 + $2 ))
}
add 3 5#!/bin/bash say_hello() { echo "Hi there!" } say_hello say_hello
greet in bash?function name() { } or name() { } syntax.def greet() { echo "Hello"; } is Python syntax, greet() => { echo "Hello"; } is JavaScript arrow function syntax, func greet() { echo "Hello"; } uses invalid keyword. Only function greet() { echo "Hello"; } is valid bash syntax.say_hello() {
echo "Hello, $1!"
}
say_hello Alice$1 to access the first argument passed to it.say_hello Alice passes "Alice" as $1, so it prints "Hello, Alice!".myfunc {
echo "Hello"
}myfunc().Pass the test and this pattern moves unknown