Introduction
Functions let you group commands to reuse them easily. Calling a function runs those commands whenever you need.
function_name() {
# commands
}
function_namegreet() {
echo "Hello!"
}
greetsay_name() {
echo "My name is $1"
}
say_name Alice#!/bin/bash say_hello() { echo "Hello, friend!" } say_hello
greet?greet on a line by itself. matches the correct way: just the function name alone.greet on a line by itself. -> Option Chello in Bash?name() { commands; } or with the keyword function followed by name and braces.hello() { echo "Hi" }, which is correct. hello { echo "Hi" } misses the () after the name. def hello() { echo "Hi" } uses Python syntax. function hello() echo "Hi" misses braces.say_hello() {
echo "Hello, $1!"
}
say_hello Alicesay_hello prints "Hello," followed by the first argument $1.$1 becomes "Alice".greet() {
echo "Hi $name"
}
greet John$name, but no variable named name is set or passed.$1 to access it.sum that takes two numbers as arguments and prints their sum. Which script correctly calls the function and prints the result?$((expression)). Inside, use $1 and $2 for arguments.sum() {
echo $(($1 + $2))
}
sum 3 5 correctly sums $1 + $2. sum() {
echo $1 + $2
}
sum 3 5 prints literal string with plus sign. sum() {
echo $((1 + 2))
}
sum 3 5 adds 1 and 2 literally, ignoring arguments. sum() {
echo $1 $2
}
sum 3 5 prints arguments separated by space.Pass the test and this pattern moves unknown