Reputation: 900
I have a while loop which reads lines from a file using read line. Then I want to check if the line is empty or not, how can I do it? I already found questions about lines with space or about a variable in this site.
Upvotes: 4
Views: 1225
Reputation: 238086
The -n
operator checks if a string is not empty:
while read line
do
if [ -n "$line" ]
echo $line
fi
done < file.txt
If you'd want to exclude strings containing only whitespace, you could use bash's pattern replacement ${var//find/replacement}
. For example:
if -n [ "${line//[[:space:]]/}" ]
Upvotes: 1
Reputation: 455020
You can use the test:
[ -z "$line" ]
From the bash man page:
-z string
True if the length of string is zero.
Upvotes: 7