Bash one-liner for "if a file exists, read it into variable, else empty string"?

Viewed 614

I can write:

X=""
if [ -f foo.txt ]; then
  X=$(<foo.txt)
fi

but this is 4 lines. Is there a more compact way to express this logic?

2 Answers

If you do just X=$(< foo.txt), X will be empty if the file doesn't exist, but you'll get an error message:

$ X=$(< foo.txt)
-bash: foo.txt: No such file or directory

If you want to suppress that (but also any other error message), you can redirect stderr to /dev/null:

{ X=$(< too.txt); } 2> /dev/null

X is in fact empty afterwards:

$ declare -p X
declare -- X="

You may want to use this one lines using success/error after file existence check:

[[ -f foo.txt ]] && s=$(<foo.txt) || s=''

s=$(<foo.txt) will be executed if file exists otherwise s='' gets executed.

Related