LFCS · Question #535
Which of the following bash options will prevent an administrator from overwriting a file with a ">"?
The correct answer is C. set -o noclobber. The set -o noclobber bash option prevents accidental overwriting of existing files when using redirection operators like >.
Question
Options
- Aset -o safe
- Bset -o noglob
- Cset -o noclobber
- Dset -o append
- Eset -o nooverwrite
How the community answered
(18 responses)- B6% (1)
- C89% (16)
- D6% (1)
Why each option
The `set -o noclobber` bash option prevents accidental overwriting of existing files when using redirection operators like `>`.
`set -o safe` is not a standard bash option for preventing file overwrites; it is not recognized by the bash shell.
`set -o noglob` disables pathname expansion (globbing), meaning characters like `*`, `?`, and `[]` are treated as literal characters, which is unrelated to preventing file overwrites.
The `noclobber` option specifically prohibits the redirection operators `>` and `>&` from overwriting an existing file. If an attempt is made to overwrite an existing file with `>`, the shell will report an error instead of destroying the file's contents, providing a safeguard against unintended data loss.
`set -o append` is not a standard bash option; `>>` is the operator for appending, and `noclobber` would still apply if attempting to create a new file with `>>` that already exists (though it primarily targets `>`).
`set -o nooverwrite` is not a standard bash option for preventing file overwrites; no such option exists in bash.
Concept tested: Bash shell `noclobber` option
Source: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#The-Set-Builtin
Topics
Community Discussion
No community discussion yet for this question.