Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Function definition in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bash Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple Bash function call
What is the output of this Bash script?
Bash Scripting
greet() {
  echo "Hello, $1!"
}
greet World
AHello, World!
Bgreet World
CHello, $1!
DWorld
Attempts:
2 left
💡 Hint
Look at how the function uses the first argument $1 inside echo.
💻 Command Output
intermediate
2: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 $result
A34
Badd 3 4
C3 4
D7
Attempts:
2 left
💡 Hint
The function echoes the sum of two numbers, which is captured in result.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Bash function
Which option contains the correct syntax for defining a Bash function?
Agreet() { echo "Hi" }
Bgreet() echo "Hi"
Cfunction greet() { echo "Hi" }
Dfunction greet { echo "Hi" }
Attempts:
2 left
💡 Hint
A Bash function needs parentheses and braces properly placed.
🔧 Debug
advanced
2: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
AThe variable msg is local to the function and not visible outside.
BThe function myfunc is never called.
CThe echo command is inside the function but not executed.
DThe variable msg is misspelled.
Attempts:
2 left
💡 Hint
Think about variable scope inside and outside functions.
🚀 Application
expert
3: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'?
A
check_file() {
  if [[ -r "$1" ]]; then
    echo "File OK"
  else
    echo "File missing or unreadable"
  fi
}
B
check_file() {
  if test -r "$1"; then
    echo "File OK"
  else
    echo "File missing or unreadable"
  fi
}
C
check_file() {
  if [ -f "$1" -a -r "$1" ]; then
    echo "File OK"
  else
    echo "File missing or unreadable"
  fi
}
D
check_file() {
  if [ -e "$1" ] && [ -r "$1" ]; then
    echo "File OK"
  else
    echo "File missing or unreadable"
  fi
}
Attempts:
2 left
💡 Hint
Check both if the file exists and is readable using test operators.

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

  1. Step 1: Understand bash function syntax

    Bash functions are defined using either name() { commands; } or function name { commands; }.
  2. 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.
  3. Final Answer:

    greet() { echo "Hello"; } -> Option A
  4. 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

  1. Step 1: Recall valid bash function syntax

    Bash allows defining functions as function name() { commands; } or name() { commands; }.
  2. 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.
  3. Final Answer:

    function myFunc() { echo "Hi"; } -> Option A
  4. 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

  1. Step 1: Understand function call with argument

    The function say_hello takes one argument and prints it using $1.
  2. Step 2: Substitute argument and print

    Calling say_hello Alice replaces $1 with "Alice", so output is "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option B
  4. 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

  1. Step 1: Check function syntax with 'function' keyword

    Bash allows function name() { commands; } or function name { commands; }. Parentheses after the name are optional.
  2. Step 2: Identify if there's an error

    The code function greet { echo "Hi"; } is valid syntax.
  3. Final Answer:

    No error; parentheses are optional with 'function' keyword -> Option D
  4. 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

  1. Step 1: Understand arithmetic in bash functions

    To add numbers, use arithmetic expansion $(()) inside echo.
  2. 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.
  3. Final Answer:

    sum() { echo $(($1 + $2)); } -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard