Script stops running after assigning empty string for a variable

Hi,

This is the first time I see something like this, and I don't why it happens.
Please give me some help. I am really appreciate it.

Basically I am trying to remove all empty lines of an input..

#!/bin/bash

set -e
set -x

str1=`echo -e "\nhaha" | grep -v ^$`
#str2=`echo -e "\n" | grep -v ^$`
echo "done"

This works fine for str1 (aka I can see "done" as output)
However, if I use str2, after assigning empty string for str2. The script stops there. And I do not see any output at all.

Thanks a lot in advance.
yoyomano

try to echo the values of str1 and str2.. It works for me ..

$ cat filename
str1=`echo -e "\nhaha" | grep -v ^$`
echo "STR1: $str1"
str2=`echo -e "\n" | grep -v ^$`
echo "STR2: $str2"
[ "$str2" = "" ] && echo "str2 is NULL" || echo "str2 not NULL"
echo "done"
$
$ bash filename
STR1: haha
STR2:
str2 is NULL
done
$

I have managed to use sed instead of grep.

I just change from

grep -v ^$

to

sed '/^$/d'

However, I will be really grateful if someone can explain why grep doesn't work.

Thanks a lot in advance.,

grep is actually working..why do you think so?

echo -e "\n"|grep -v ^$

is equal to

echo -e "\n"|sed '/^$/d'

both of these give same result so is NULL.

The exit code of the assignment is the exit code of the last command of the pipe line which is grep. Since grep didn't find any matching data, it exited with 1. The shell script exited since you have set -e at the beginning.