Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

In a Bash script, which THREE of the following are valid ways to define a function? (Select THREE.)

⚠ Common exam trap

Candidates often think the 'function' keyword requires parentheses or that parentheses alone are insufficient, but Bash accepts both with and without the keyword, leading to confusion about which combinations are valid.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

function myfunc() { commands; }

Option A, `function myfunc() { commands; }`, is correct because Bash accepts the `function` keyword combined with parentheses, a hybrid syntax that is valid in Bash (though not POSIX sh). Option C, `function myfunc { commands; }`, is correct because the `function` keyword alone followed by the name and a compound command body is a valid Bash function definition form. Option E, `myfunc() { commands; }`, is correct because the POSIX-standard form using the name, empty parentheses, and a brace-delimited body is fully supported by Bash. Option B is invalid because `myfunc = () { commands; }` includes an illegal space and equals sign, which Bash would treat as a command assignment, not a function definition. Option D is invalid because `def` is not a Bash keyword; it is used in Python, so Bash would attempt to run `def` as a command and fail.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    function myfunc() { commands; }

    Why this is correct

    Bash accepts the hybrid syntax combining the function keyword with parentheses and braces. The shell parses function myfunc() { commands; } as a valid definition, so this form satisfies the stem's requirement for a legitimate function declaration alongside the other two accepted variants.

  • ✗

    myfunc = () { commands; }

    Why it's wrong here

    Bash rejects spaces around the equals sign and the empty parentheses, so this is a syntax error. It tempts those recalling variable-assignment syntax, where `name = value` is likewise invalid; the valid form is `myfunc() { commands; }` with no spaces.

  • ✓

    function myfunc { commands; }

    Why this is correct

    Bash permits the function keyword without parentheses, so function myfunc { commands; } is parsed as a valid definition. The braces delimit the body and the name follows the keyword directly, satisfying the stem's requirement for a legitimate declaration form.

  • ✗

    def myfunc { commands; }

    Why it's wrong here

    Bash has no `def` keyword; that belongs to Python, so this line fails to parse. It tempts candidates mixing Python function syntax into shell scripting, where the correct definition is `myfunc() { commands; }` or `function myfunc { commands; }`.

  • ✓

    myfunc() { commands; }

    Why this is correct

    The POSIX-style form myfunc() { commands; } is the most portable Bash function definition, requiring no function keyword. Bash parses the name, empty parentheses and brace-delimited body as a valid declaration, satisfying the stem's requirement for one of the three accepted syntaxes.

About these practice questions

One of 781 original XK0-006 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official CompTIA exam blueprint

This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.