In bash, a command link
echo test > actual.txt
will replace the contents of the file called
actual.txt with "test", and create the file if it doesn't exist. However, if the file does exist, bash will just open it, truncate it, and write the new contents into the file.
Specifically, the redirect command fails in this scenario:
ln -s /some/illegal/path link.txt echo test > link.txt
Bash 4.4.12 gives me the confusing error message
link.txt: No such file or directory.
One way to avoid this is to make sure to delete the file before running the redirected command.
rm link.txt && echo test > link.txt
I was wondering, though, if there was some tweak of bash options or the redirect operator which will prevent this failure mode. Any ideas?