In Linux - check if there is an empty line at the end of a file, some posts make use of [[ ]]
and ==
to compare characters.
I would like to write a one-line command for detecting if there's no newline at EOF, and I came across this little problem.
In the output of echo
, there's \n
at the end.
$ echo echo | od -c
0000000 e c h o \n
0000005
$ echo -n echo | od -c
0000000 e c h o
0000004
If I put [[ ]]
and ==
together, then I don't get the expected output.
$ [[ `echo echo | tail -c1` == "\n" ]] && echo true
$ [[ `echo echo | tail -c1` != "\n" ]] && echo true
true
$ [[ `echo -n echo | tail -c1` != "\n" ]] && echo true
true
As shown by od -c
, the output of echo echo | tail -c1
is \n
, and [[ "\n" == "\n" ]] && true
would return true
, so I expect the first command gives true
. However, why is it evaluated to empty string?
Thanks for reading!