LFCS · Question #730
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 remove a directory whose name contains a literal backslash, the backslash character must be escaped with another backslash to prevent the shell from interpreting it as an escape sequence.
Question
Options
- Armdir '~/\dir'
- Brmdir "~/\dir"
- Crmdir ~/'dir'
- Drmdir ~/\dir
- Ermdir ~/\dir
How the community answered
(51 responses)- A6% (3)
- B2% (1)
- C2% (1)
- D12% (6)
- E78% (40)
Why each option
To remove a directory whose name contains a literal backslash, the backslash character must be escaped with another backslash to prevent the shell from interpreting it as an escape sequence.
Single quotes `''` prevent most shell expansions, but the `~` (home directory) expansion usually occurs before quoting. However, `\dir` inside single quotes will treat `\dir` literally as part of the path, which is not correct for removing `\dir` where the first `\` is a literal. The shell needs to see `\\` to interpret `\` as part of the directory name.
Double quotes `""` suppress some shell expansions but `\` is still an escape character within them. For a literal backslash, it still needs to be escaped as `\\`.
This command would attempt to remove a directory named `dir` (without a leading backslash) in the home directory, which is not the target.
Without quoting or escaping, the `\` would be interpreted by the shell as an escape character for the 'd', meaning `\dir` would likely be interpreted as `dir`, or result in a syntax error.
The backslash (`\`) is an escape character in the shell. To specify a literal backslash in a directory name like `\dir`, the backslash itself must be escaped, resulting in `~/\\dir` for `rmdir` to correctly interpret the path.
Concept tested: Shell escaping special characters
Source: https://www.gnu.org/software/bash/manual/bash.html#Quoting
Topics
Community Discussion
No community discussion yet for this question.