I am attempting to remove lines from this text file (foo.txt):
cat
mouse
animals: 2
I want to remove the two lines from the end:
animals: 2
So I end up with this:
cat
mouse
However, when using variable="$(sed '$d' foo.txt)", both lines appear to be removed, even though I only specified one $d. Even though this gives me the result I am looking for, I can't help but wonder why this is happening. When attempting to NOT store the output in a variable, I get the output I expect to see:
user$ sed '$d' foo.txt
cat
mouse
user$
Oddly enough, using $ sed '$d;$d' foo.txt only removes the animals: 2 line, and not the newline before it.
Could somebody de-mystify this for me? Thanks.
For the record, BSD sed is being used in this case.