I'm trying to cache list of files in a variable:
files=`ls -1 | head -2`
echo $files
So that if there's only one file -- use it:
files=`ls -1 | head -2`
numberOfFiles=`echo $files | wc -l`
if [ $numberOfFiles == 1 ]; then
echo "Using $files"
else
echo "ERROR: Too many files:\n$files\nI don't know which one to take."
fi
But much to my surprise echo $files
does not preserve new lines of ls -1
output. So $numberOfFiles
is always 1.
How do I store multiline output of ls -1
in a variable?