A function is a named block of code that performs a specific task and can be reused multiple times in a script.
Click to reveal answer
beginner
How do you define a function named greet in bash?
You can define it like this:
greet() {
echo "Hello!"
}
Click to reveal answer
beginner
How do you call a function named greet in bash?
Just write the function name without parentheses:
greet
Click to reveal answer
intermediate
Can bash functions accept arguments? How?
Yes, bash functions accept arguments which are accessed inside the function as $1, $2, etc., representing the first, second argument, and so on.
Click to reveal answer
intermediate
What is the purpose of the return statement in a bash function?
The return statement ends the function and optionally sends an exit status (a number between 0 and 255) back to the caller.
Click to reveal answer
How do you start defining a function named say_hello in bash?
Adef say_hello():
Bfunction say_hello[] {
Cfunction say_hello()
Dsay_hello() {
✗ Incorrect
In bash, functions are defined with the syntax: function_name() { ... }
Inside a bash function, how do you access the first argument passed to it?
A$0
B$first
C$1
D$arg1
✗ Incorrect
Arguments inside bash functions are accessed as $1, $2, etc. $0 is the script name.
What does the return statement do inside a bash function?
APrints a value to the screen
BEnds the function and sets an exit status
CCalls another function
DDefines a new variable
✗ Incorrect
return ends the function and optionally sends an exit status back.
Which of these is a correct way to call a bash function named greet?
Agreet
Bgreet()
Ccall greet
Drun greet
✗ Incorrect
You call a bash function simply by writing its name: greet
What symbol is used to start the body of a bash function?
A{
B[
C(
D<
✗ Incorrect
The function body starts with a curly brace { and ends with }
Explain how to define and call a function in bash, including how to pass and use arguments.
Think about how you tell a friend to do a task and give them some details.
You got /3 concepts.
Describe the role of the return statement in a bash function and how it differs from outputting text.
Return is like finishing a task and giving a status, echo is like saying something out loud.
You got /3 concepts.
Practice
(1/5)
1. What is the correct way to define a function named greet in bash?
easy
A. greet() { echo "Hello"; }
B. function greet[] { echo "Hello"; }
C. def greet() { echo "Hello"; }
D. function greet() echo "Hello"
Solution
Step 1: Understand bash function syntax
Bash functions are defined using either name() { commands; } or function name { commands; }.
Step 2: Check each option
greet() { echo "Hello"; } uses the correct syntax with parentheses and braces. Options B, C, and D have syntax errors or use wrong keywords.
Final Answer:
greet() { echo "Hello"; } -> Option A
Quick Check:
Function syntax = greet() { ... } [OK]
Hint: Use parentheses and braces to define bash functions [OK]
Common Mistakes:
Using square brackets instead of parentheses
Using Python 'def' keyword
Omitting braces around commands
2. Which of the following is a valid function definition syntax in bash?
easy
A. function myFunc() { echo "Hi"; }
B. myFunc[] { echo "Hi"; }
C. function myFunc[] echo "Hi"
D. myFunc() echo "Hi"
Solution
Step 1: Recall valid bash function syntax
Bash allows defining functions as function name() { commands; } or name() { commands; }.
Step 2: Evaluate options
function myFunc() { echo "Hi"; } matches valid syntax. Options A and C use square brackets incorrectly. myFunc() echo "Hi" misses braces around commands.
Final Answer:
function myFunc() { echo "Hi"; } -> Option A
Quick Check:
Valid syntax includes 'function name() { ... }' [OK]
Hint: Remember to use braces {} to group commands in functions [OK]
Common Mistakes:
Using square brackets instead of parentheses
Omitting braces after function name
Missing parentheses after function name
3. What will be the output of this bash script?
say_hello() {
echo "Hello, $1!"
}
say_hello Alice
medium
A. Error: function not found
B. Hello, Alice!
C. say_hello Alice
D. Hello, $1!
Solution
Step 1: Understand function call with argument
The function say_hello takes one argument and prints it using $1.
Step 2: Substitute argument and print
Calling say_hello Alice replaces $1 with "Alice", so output is "Hello, Alice!".
Final Answer:
Hello, Alice! -> Option B
Quick Check:
Function argument $1 = Alice [OK]
Hint: Use $1 to access first argument inside bash functions [OK]
Common Mistakes:
Printing literal $1 instead of its value
Forgetting to call the function
Syntax errors in function definition
4. Identify the error in this bash function definition:
function greet {
echo "Hi"
}
medium
A. Missing parentheses after function name
B. Missing braces around commands
C. Missing semicolon after echo
D. No error; parentheses are optional with 'function' keyword
Solution
Step 1: Check function syntax with 'function' keyword
Bash allows function name() { commands; } or function name { commands; }. Parentheses after the name are optional.
Step 2: Identify if there's an error
The code function greet { echo "Hi"; } is valid syntax.
Final Answer:
No error; parentheses are optional with 'function' keyword -> Option D
Quick Check:
function name { ... } [OK]
Hint: Parentheses () are optional with 'function name' [OK]
Common Mistakes:
Omitting parentheses after function name
Confusing braces and parentheses
Assuming semicolon needed after echo
5. You want to create a bash function sum that takes two numbers and prints their sum. Which function definition correctly does this?
hard
A. function sum { echo $1 + $2; }
B. sum() { echo $1 + $2; }
C. sum() { echo $(($1 + $2)); }
D. sum() { echo $1 $2; }
Solution
Step 1: Understand arithmetic in bash functions
To add numbers, use arithmetic expansion $(()) inside echo.
Step 2: Evaluate each option
sum() { echo $(($1 + $2)); } correctly sums $1 and $2 using $(($1 + $2)). Options A and B print strings with plus sign, not sum. sum() { echo $1 $2; } prints numbers without adding.
Final Answer:
sum() { echo $(($1 + $2)); } -> Option C
Quick Check:
Use $(()) for arithmetic in bash functions [OK]
Hint: Use $(()) to perform math inside bash functions [OK]
Common Mistakes:
Printing variables without arithmetic expansion
Omitting parentheses for function definition
Using 'function' without parentheses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.