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.
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)- A4% (2)
- B4% (2)
- C16% (8)
- D2% (1)
- E74% (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.
Single quotes prevent tilde expansion, so '~' is treated as a literal character rather than the home directory path, making the path incorrect.
Double quotes also prevent tilde expansion in bash, so the path does not resolve to the home directory.
This expands to '~/dir' without a backslash because the backslash is inside single quotes applied only to 'dir', not to the backslash itself.
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'.
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
Community Discussion
No community discussion yet for this question.