A function is a named block of code that performs a specific task and can be called multiple times in a script to avoid repetition.
Click to reveal answer
beginner
How do you call a function named greet in bash?
Simply write the function name alone: greet. Parentheses are not used when calling functions in bash.
Click to reveal answer
intermediate
What happens if you call a function before defining it in a bash script?
Bash will give an error because the function must be defined before it is called in the script.
Click to reveal answer
beginner
How can you pass arguments to a bash function?
You pass arguments by writing them after the function name when calling it. Inside the function, use $1, $2, etc., to access them.
Click to reveal answer
intermediate
What is the difference between calling a function with and without parentheses in bash?
In bash, calling a function with or without parentheses is not the same. You call a function by its name without parentheses, e.g., func. Using parentheses like func() is a syntax error.
Click to reveal answer
How do you call a function named hello in bash?
Acall hello
Bhello
Cfunction hello()
Drun hello
✗ Incorrect
In bash, you call a function simply by writing its name.
What will happen if you call a function before defining it in a bash script?
AThe function runs normally
BThe script ignores the call
CThe function is defined automatically
DBash throws an error
✗ Incorrect
Bash requires functions to be defined before they are called, or it will throw an error.
How do you access the first argument passed to a bash function?
A$1
B$first
C$arg1
D$0
✗ Incorrect
Inside a bash function, the first argument is accessed using $1.
Which of these is a valid way to call a function named myfunc?
Acall myfunc()
Brun myfunc
Cmyfunc()
Dfunction myfunc
✗ Incorrect
Calling a function with parentheses like myfunc() is valid in bash.
Can you call a bash function without parentheses?
AYes, parentheses are optional
BNo, parentheses are required
COnly if the function has no arguments
DOnly in bash version 5 or higher
✗ Incorrect
In bash, parentheses are optional when calling functions.
Explain how to define and call a function in bash with an example.
Think about how you write a function and then use its name to run it.
You got /3 concepts.
Describe how to pass and access arguments inside a bash function.
Arguments are like inputs you give when calling the function.
You got /3 concepts.
Practice
(1/5)
1. In Bash scripting, how do you call a function named greet?
easy
A. Write greet() with parentheses.
B. Write call greet().
C. Just write greet on a line by itself.
D. Write run greet.
Solution
Step 1: Understand Bash function call syntax
In Bash, functions are called by simply writing their name without parentheses or keywords.
Step 2: Compare options with Bash syntax
Only Just write greet on a line by itself. matches the correct way: just the function name alone.
Final Answer:
Just write greet on a line by itself. -> Option C
Quick Check:
Function call = function name alone [OK]
Hint: Call functions by name only, no parentheses needed [OK]
Common Mistakes:
Adding parentheses like in other languages
Using keywords like 'call' or 'run'
Forgetting to write the function name
2. Which of the following is the correct syntax to define a function named hello in Bash?
easy
A. hello { echo "Hi" }
B. hello() { echo "Hi" }
C. def hello() { echo "Hi" }
D. function hello() echo "Hi"
Solution
Step 1: Recall Bash function definition syntax
Bash functions are defined as name() { commands; } or with the keyword function followed by name and braces.
Step 2: Check each option
hello() { echo "Hi" } uses 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.
Final Answer:
hello() { echo "Hi" } -> Option B
Quick Check:
Function syntax = name() { commands } [OK]
Hint: Use name() { commands } to define functions in Bash [OK]
Common Mistakes:
Using Python or other language syntax
Omitting parentheses or braces
Misplacing the function keyword
3. What is the output of this script?
say_hello() {
echo "Hello, $1!"
}
say_hello Alice
medium
A. Hello, Alice!
B. Hello, $1!
C. say_hello Alice
D. Error: function not found
Solution
Step 1: Understand function and argument usage
The function say_hello prints "Hello," followed by the first argument $1.
Step 2: Identify the argument passed
The function is called with argument "Alice", so $1 becomes "Alice".
Final Answer:
Hello, Alice! -> Option A
Quick Check:
Function argument $1 = Alice [OK]
Hint: Use $1 to access first argument inside function [OK]
Common Mistakes:
Printing literal $1 instead of its value
Forgetting to pass argument
Misunderstanding function call syntax
4. Identify the error in this script:
greet() {
echo "Hi $name"
}
greet John
medium
A. Function greet is not defined correctly
B. Missing semicolon after echo
C. Function call syntax is wrong
D. Variable $name is not set or passed
Solution
Step 1: Check variable usage inside function
The function uses $name, but no variable named name is set or passed.
Step 2: Check function call and argument passing
The function is called with "John" as argument, but the function does not use $1 to access it.
Final Answer:
Variable $name is not set or passed -> Option D
Quick Check:
Use $1 to access arguments, not undefined variables [OK]
Hint: Use $1 inside function to get passed arguments [OK]
Common Mistakes:
Using undefined variables instead of $1
Assuming arguments auto-assign to variable names
Ignoring function call syntax correctness
5. You want to write a Bash function sum that takes two numbers as arguments and prints their sum. Which script correctly calls the function and prints the result?
hard
A.
sum() {
echo $(($1 + $2))
}
sum 3 5
B.
sum() {
echo $1 + $2
}
sum 3 5
C.
sum() {
echo $((1 + 2))
}
sum 3 5
D.
sum() {
echo $1 $2
}
sum 3 5
Solution
Step 1: Understand arithmetic evaluation in Bash
To add numbers, use $((expression)). Inside, use $1 and $2 for arguments.
Step 2: Check each option's output
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.
Final Answer:
sum() { echo $(($1 + $2)) } sum 3 5 -> Option A
Quick Check:
Use $(( $1 + $2 )) for sum [OK]
Hint: Use $(( $1 + $2 )) to add arguments inside function [OK]
Common Mistakes:
Printing arguments without arithmetic evaluation
Using fixed numbers instead of arguments
Confusing string concatenation with addition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.