0
0
Bash Scriptingscripting~20 mins

Calling functions in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple function call
What is the output of this Bash script?
Bash Scripting
greet() {
  echo "Hello, $1!"
}
greet World
AHello, World!
Bgreet World
CWorld
DHello, $1!
Attempts:
2 left
💡 Hint
Look at how the function uses the first argument.
💻 Command Output
intermediate
2:00remaining
Function with local variable scope
What will be the output of this script?
Bash Scripting
count=5
myfunc() {
  local count=10
  echo $count
}
myfunc
echo $count
A
5
5
B
5
10
C
10
10
D
10
5
Attempts:
2 left
💡 Hint
Remember local variables inside functions do not change outside variables.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in function definition
Which option contains a syntax error in defining a Bash function?
Amyfunc() { echo "Hello"; }
Bmyfunc() echo "Hello"
Cfunction myfunc { echo "Hello"; }
Dfunction myfunc() { echo "Hello"; }
Attempts:
2 left
💡 Hint
Check if the function body is properly enclosed.
🔧 Debug
advanced
2:00remaining
Why does this function not print the argument?
Given this script, why does it print nothing? myfunc() { echo $name } myfunc John
AThe function does not use $1 to access the argument
BThe function name is invalid
CThe echo command is missing quotes
DThe function is not called correctly
Attempts:
2 left
💡 Hint
How do functions access passed arguments in Bash?
🚀 Application
expert
2:00remaining
Function calling another function with output capture
What is the output of this script?
Bash Scripting
get_date() {
  date +"%Y-%m-%d"
}
print_message() {
  local today=$(get_date)
  echo "Today is $today"
}
print_message
AToday is
BToday is $(get_date)
CToday is 2024-06-01
Dget_date: command not found
Attempts:
2 left
💡 Hint
The inner function output is captured with $(...).