nerdexam
CompTIA

XK0-006 · Question #35

In the echo "profile-$num-$name" line of a shell script, the variable $num seems to not be expanding during execution. Which of the following notations ensures the value is expanded?

The correct answer is D. echo "profile-${num}-$name". Using ${num} ensures correct variable expansion, especially when variables are adjacent to other characters or hyphens. This prevents ambiguity and guarantees $num is properly expanded within the string.

Automation, Orchestration, and Scripting

Question

In the echo "profile-$num-$name" line of a shell script, the variable $num seems to not be expanding during execution. Which of the following notations ensures the value is expanded?

Options

  • Aecho "profile-$(num)-$name"
  • Becho 'profile-$num-$name'
  • Cecho "profile-'Snum'-$name"
  • Decho "profile-${num}-$name"

How the community answered

(56 responses)
  • A
    4% (2)
  • B
    14% (8)
  • C
    9% (5)
  • D
    73% (41)

Explanation

Using ${num} ensures correct variable expansion, especially when variables are adjacent to other characters or hyphens. This prevents ambiguity and guarantees $num is properly expanded within the string.

Topics

#shell scripting#variable expansion#bash syntax#string interpolation

Community Discussion

No community discussion yet for this question.

Full XK0-006 Practice