The most promising thing I can think of is a loop:
for f in file1 file2; do if [ -f "$f" ]; then var="$f" break fidone
You can shorten the code by using &&
if you like, at the expense of supporting the errexit
shell option. For just two files, the benefits of this approach are questionable, since it's about the same amount of code as the if
/elif
statement you already wrote, and it's probably marginally less clear what it's trying to do. But for many files this will be significantly more concise.
And as others have suggested, I think this is definitely a good thing to wrap up in a shell function. Keep in mind that variable assignments are not (necessarily) scoped to shell functions in bash, so you'll have no problem setting a variable from inside the function and using it elsewhere; however, you could also consider having the function print the name of the found file to standard output if that's more useful to you.
If you put this in a function, you can actually use the shell's built-in ability to loop over positional parameters, like this:
find_file() { for f; do if [ -f "$f" ]; then var="$f" return fi done}