Challenge - 5 Problems
Function Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of function using positional arguments
What is the output of this Bash script?
Bash Scripting
my_func() {
echo "First: $1, Second: $2"
}
my_func apple bananaAttempts:
2 left
💡 Hint
Remember that $1 and $2 inside a function refer to the function's arguments.
✗ Incorrect
Inside the function, $1 is the first argument passed to it, which is 'apple', and $2 is the second argument, 'banana'.
💻 Command Output
intermediate2:00remaining
Effect of shifting arguments inside a function
What will this script output?
Bash Scripting
my_func() {
shift
echo "$1 $2"
}
my_func one two threeAttempts:
2 left
💡 Hint
The shift command moves all arguments one position to the left.
✗ Incorrect
After shift, $1 becomes 'two' and $2 becomes 'three'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in function argument usage
Which option contains a syntax error when trying to print the first argument inside a function?
Bash Scripting
my_func() {
echo $1
}
my_func testAttempts:
2 left
💡 Hint
Check how positional parameters are referenced in Bash.
✗ Incorrect
Option A uses $[1], which is arithmetic expansion syntax and invalid for positional parameters.
🔧 Debug
advanced2:00remaining
Why does this function print empty values?
Given this script, why does the function print empty lines?
Bash Scripting
my_func() {
echo "$1 $2"
}
my_func
Attempts:
2 left
💡 Hint
Think about what happens if you call a function without arguments.
✗ Incorrect
Since no arguments are passed, $1 and $2 inside the function are empty strings.
🚀 Application
expert3:00remaining
How to pass all script arguments to a function preserving spaces
You want to pass all arguments given to the script into a function, preserving spaces in each argument. Which option correctly does this?
Bash Scripting
my_func() {
for arg in "$@"; do
echo "$arg"
done
}
# How to call my_func with all script arguments?Attempts:
2 left
💡 Hint
Use quotes to preserve each argument as a separate word.
✗ Incorrect
Using "$@" passes each argument as a separate quoted string, preserving spaces correctly.