nerdexam
CompTIA

LX0-103 · Question #130

A user accidentally created the subdirectory \dir in his home directory. Which of the following commands will remove that directory?

The correct answer is E. rmdir ~/\\dir. To reference a directory whose name contains a literal backslash in bash, the backslash must be escaped with a second backslash when used outside of quotes.

GNU and Unix Commands

Question

A user accidentally created the subdirectory \dir in his home directory. Which of the following commands will remove that directory?

Options

  • Armdir '~/\dir'
  • Brmdir "~/\dir"
  • Crmdir ~/'dir'
  • Drmdir ~/\dir
  • Ermdir ~/\dir

How the community answered

(50 responses)
  • A
    4% (2)
  • B
    4% (2)
  • C
    16% (8)
  • D
    2% (1)
  • E
    74% (37)

Why each option

To reference a directory whose name contains a literal backslash in bash, the backslash must be escaped with a second backslash when used outside of quotes.

Armdir '~/\dir'

Single quotes prevent tilde expansion, so '~' is treated as a literal character rather than the home directory path, making the path incorrect.

Brmdir "~/\dir"

Double quotes also prevent tilde expansion in bash, so the path does not resolve to the home directory.

Crmdir ~/'dir'

This expands to '~/dir' without a backslash because the backslash is inside single quotes applied only to 'dir', not to the backslash itself.

Drmdir ~/\dir

A single unquoted backslash before 'd' is a bash escape sequence that simply produces the literal character 'd' and discards the backslash, so this resolves to '~/dir' instead of '~/\dir'.

Ermdir ~/\\dirCorrect

In bash, an unquoted '\\' represents a single literal backslash character, so '~/\\dir' expands to the home directory path followed by '\dir', which matches the actual directory name. The tilde must remain unquoted to trigger tilde expansion to the home directory path. This combination correctly encodes both the home directory and the literal backslash in the name.

Concept tested: shell escaping backslash characters in directory names

Source: https://www.gnu.org/software/bash/manual/bash.html#Quoting

Topics

#rmdir#special characters#shell quoting#backslash escaping

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice