Challenge - 5 Problems
Bash Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a simple Bash function call
What is the output of this Bash script?
Bash Scripting
greet() {
echo "Hello, $1!"
}
greet WorldAttempts:
2 left
💡 Hint
Look at how the function uses the first argument $1 inside echo.
✗ Incorrect
The function greet takes the first argument and prints it with Hello. Calling greet World passes 'World' as $1, so output is 'Hello, World!'.
💻 Command Output
intermediate2:00remaining
Return value from a Bash function
What is the output of this Bash script?
Bash Scripting
add() {
echo $(( $1 + $2 ))
}
result=$(add 3 4)
echo $resultAttempts:
2 left
💡 Hint
The function echoes the sum of two numbers, which is captured in result.
✗ Incorrect
The add function calculates the sum of $1 and $2 and echoes it. The output is captured in 'result' and then printed, so the output is 7.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this Bash function
Which option contains the correct syntax for defining a Bash function?
Attempts:
2 left
💡 Hint
A Bash function needs parentheses and braces properly placed.
✗ Incorrect
Options A and C use correct syntax: function name with parentheses followed by braces enclosing the commands. Option D is also valid in Bash but less common.
🔧 Debug
advanced2:00remaining
Why does this Bash function not print the expected output?
Given this script, what is the reason it prints nothing?
myfunc() {
local msg="Hello"
}
myfunc
echo $msg
Attempts:
2 left
💡 Hint
Think about variable scope inside and outside functions.
✗ Incorrect
The variable msg is declared local inside the function, so it does not exist outside. The echo command outside prints an empty value.
🚀 Application
expert3:00remaining
Function to check if a file exists and is readable
Which function definition correctly checks if a file exists and is readable, then prints 'File OK' or 'File missing or unreadable'?
Attempts:
2 left
💡 Hint
Check both if the file exists and is readable using test operators.
✗ Incorrect
Option C uses [ -f "$1" -a -r "$1" ] which checks if the file exists and is a regular file and readable. This is the most precise check.