As an alternative without the problem of spaces (POSIX):

    $ sed -n '$=' ~/myfile.txt

Captured in a variable:

    $ nl=$(sed -n '$=' ~/myfile.txt)
    $ echo "1-$nl-"

Note that sed counts the last line even if it doesn't end in a newline character (as it should in a properly formatted text file). More generally, sed may fail to print the number of newline characters in non-text files.

Another alternative:

    $ nl=$(grep -v . ~/myfile.txt | tr '\n' 'x')
    $ echo "1-${#nl}"

Note that grep will also print invalid characters (additionally to newlines) in the locale used.