zsh, you can use the
$mapfile special associative array that maps to the content of the file named as the key (note that it's not related to and predates
mapfile builtin (which doesn't map files) by a decade or so). It uses
mmap() so can only be used on regular files.
It doesn't output anything if the file cannot be opened or mmaped. To check for errors, you can use the
$ERRNO special variable:
if ((ERRNO)); then
: the mapping failed
$ERRNO values can be translated to
EACCESS... codes with the
$errnos array in the
zsh/system module, or to error strings with the
syserror builtin in
zsh/system as well.
You can also do
mapfile[filename]=$var to change the content of the file, it is a true mapping.
Note that while
$(<filename) would strip all trailing newline characters from the content of
$mapfile[filename] expands to the whole content of the file (including binary files) as-is.