When I'm using one of the comparison operators/functions on an empty file, Bash returns that the file is not empty. There must be a newline character or something, but it's making all these tests pass when they shouldn't.
So is there another way to test if a file actually has a character? I've tried [ -s file.txt ], [ -n file.txt], and all its brethren, but they all return that the file.txt is not empty.
I've tried doing cat and assigning it to a variable, but that variable is read as not empty for some reason.
Any other way to see if a file is truly empty?
edit
So here's what I've done. I cleared the file (ctrl+a and delete). Made sure when I tried moving cursor that the cursor doesn't move. Then I did if [ -n filename ] ; then echo "not empty"; fi; This returns not empty
-stells you if the file is entirely empty i.e. has no contents. If you need something else you'll need to figure out what you need and how to test for it.[ -s file.txt ]", can you say "I've tried this:> file.txt; ls -l file.txt; [ -s file.txt ] && echo "the file is empty"but it shows-rw-r------ 1 me me 0 Jun 18 10:32 file.txtwhile I expected it to also show "the file is empty" because the file size is 0"?ctrl+aanddeletemean? Which editor are you using? What does it save when you save that empty file? What doesls -lsay is the size of the file? At this point, it sounds like it might be an editor problem, not a shell problem. Does your editor create empty files?ctrl+aanddeleteto select everything and delete it. I'm using Notepad++ andls -lshows0bytes, Windows also shows 0 bytes. I'm using Cygwin on windows-noperator is a string test, not a file test. What happens if you replace-nwith-s?