Courseiva
easyMultiple Choice

XK0-006 Practice Question: A script named 'test.sh' contains '#!/bin/bash'…

A script named 'test.sh' contains '#!/bin/bash' and is located in the current directory. Which command runs the script in the current shell environment without forking a subshell?

⚠ Common exam trap

CompTIA often tests the distinction between executing a script via its path (which forks a subshell) and sourcing it (which runs in the current shell), and candidates mistakenly think that `./test.sh` runs in the current shell because it is invoked directly from the command line.

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

✓

source test.sh

The `source` command (or its synonym `.`) executes the script in the current shell environment without forking a subshell. This is essential when you need the script to modify the current shell's environment, such as setting variables or changing directories, because a subshell would discard those changes upon exit.

Answer analysis

Option-by-option breakdown

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

  • ✗

    ./test.sh

    Why it's wrong here

    ./test.sh executes the script as a separate process via its shebang, forking a subshell, so state changes do not persist. It is tempting because it is the standard way to run an executable script, and would be correct when the script should run in its own process with its own environment.

  • ✗

    sh test.sh

    Why it's wrong here

    Invoking sh test.sh starts a new /bin/sh process, which forks a subshell, so exported variables and directory changes are lost. It is tempting because it does execute the script, and would be correct when portability across shells matters rather than preserving the current shell's environment.

  • ✗

    bash test.sh

    Why it's wrong here

    bash test.sh launches a new bash process, forking a subshell, so variable and directory changes vanish on exit. It is tempting because it explicitly uses bash and honours the script, and would be correct when you need a clean bash environment rather than modifying the current shell.

  • ✓

    source test.sh

    Why this is correct

    The source builtin reads and executes the script's commands in the current shell process, so variables and directory changes persist and no subshell is forked. Running ./test.sh or bash test.sh would spawn a child process instead.

About these practice questions

This XK0-006 question is part of Courseiva's 781-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.