Read file into variable while suppressing "No such file or directory" error
With BASH it is easy to test if file exists before reading it in
[ -e file ] && var=$(< file )
As Inian points in the comments the file might exist but the user might not have sufficient rights for reading it. The -r
test would take care of that.
[ -r file ] && var=$(< file )
var=$(cat file 2>/dev/null)
This is a Useful Use of Cat. It enforces an order of operations: stderr is redirected before file
is opened. It serves a purpose. Don't feel bad about using it.