noclobber
“Clobbering” a file has a very specific meaning in Bash — it’s when you use the >
redirect operator to overwrite an existing regular file. Setting noclobber
results in a non–zero exit code when a script tries to clobber a file, leaving the file untouched. See for example this script, intended to get 32 bytes of random binary data and convert it to hexadecimal so that it can be stored in a configuration file:
noclobber
also applies to symbolic links to regular files. Some special files like /dev/null are not actually changed when you attempt to clobber them.noclobber
is smart enough to allow such redirects.
1
#!/usr/bin/env bash
2
3
set -o errexit -o noclobber
4
5
working_directory="$(mktemp --directory)"
6
bin_path="${working_directory}/secret.bin"
7
hex_path="${working_directory}/secret.hex"
This page is a preview of The newline Guide to Bash Scripting