Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

In a bash script, a variable is set as follows: myvar='Hello World'. Which of the following correctly prints the first 5 characters of the variable?

⚠ Common exam trap

The trap is mixing up prefix/suffix pattern removal (# and %) with substring extraction (:offset:length); candidates often pick ${myvar#?????} thinking # means 'take' rather than 'strip'.

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

✓

echo ${myvar:0:5}

Bash parameter expansion supports substring extraction using the syntax ${variable:offset:length}. With myvar='Hello World', ${myvar:0:5} starts at index 0 and returns 5 characters, yielding 'Hello'. This is the standard, portable way to slice a string in Bash.

Answer analysis

Option-by-option breakdown

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

  • ✗

    echo ${myvar#?????}

    Why it's wrong here

    The # operator strips the shortest matching prefix, so ${myvar#?????} removes five characters from the front and prints 'World', not the first five. It is tempting because the five question marks suggest five characters, but substring extraction requires ${myvar:0:5}, which returns 'Hello'.

  • ✓

    echo ${myvar:0:5}

    Why this is correct

    Bash substring expansion uses ${parameter:offset:length}, so ${myvar:0:5} extracts five characters starting at index zero, yielding "Hello". The offset is zero-based, and the length is explicit, which is exactly what printing the first five characters requires.

  • ✗

    echo ${myvar:0-5}

    Why it's wrong here

    ${myvar:0-5} treats the offset as an arithmetic expression evaluating to -5, so bash counts five characters back from the end, printing 'World'. It is tempting because negative offsets do extract trailing characters, but the stem requires the leading five, which needs offset 0 with length 5.

  • ✗

    echo ${myvar:5}

    Why it's wrong here

    Substring expansion with a positive offset starts at that index, so ${myvar:5} prints from the sixth character onward, yielding ' World', not the first five. It is tempting because offset syntax does extract substrings, but the correct form needs both offset 0 and length 5.

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.