nerdexam
CompTIA

XK0-004 · Question #480

A Bash script is saving the following list of users from the home directory to a variable: User=''home/matt home/tom home/john home/mike''. Which of the following commands creates a list of only the…

The correct answer is A. Users='' (sed -r 's/home///g' <<< $Users)''. Option A uses sed with the -r flag (extended regex) and the substitution expression 's/home///g', which replaces every occurrence of the string home/ with nothing (empty string), globally across the input. The <<< here-string syntax passes the variable $Users directly to sed as…

Scripting, Containers and Automation

Question

A Bash script is saving the following list of users from the home directory to a variable:

User=''home/matt home/tom home/john home/mike''. Which of the following commands creates a list of only the usernames?

Options

  • AUsers='' (sed -r 's/home///g' <<< $Users)''
  • BUsers=$ (sed -r 's?|home/||'<<< $Users)
  • CUsers=$(Users//home\ /)
  • DUsers='sed -r 's/home\ ///g' <<< $users'

How the community answered

(46 responses)
  • A
    74% (34)
  • B
    9% (4)
  • C
    13% (6)
  • D
    4% (2)

Explanation

Option A uses sed with the -r flag (extended regex) and the substitution expression 's/home///g', which replaces every occurrence of the string home/ with nothing (empty string), globally across the input. The <<< here-string syntax passes the variable $Users directly to sed as input. The result strips home/ from each entry, leaving only the usernames: matt tom john mike. Option B uses an invalid sed syntax with ? as a delimiter inconsistently and a malformed command. Option C attempts shell parameter expansion (${Users//home//}) but is missing the ${} syntax entirely. Option D places the sed command inside single quotes instead of command substitution $(...), so it would assign the literal string - not execute the command.

Topics

#sed#bash scripting#string manipulation#regex

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice