You can't (safely) redirect a command's output to a file that it's reading. You're "lucky" that it happened to work from the command line. Probably in one case
sed was able to read the entire file before the shell clobbered it, and in the other case it wasn't. The difference is probably due to some buffering issue, but it's not really worth figuring it out.
If you want to modify a file in place, you should write the output to a temporary file, then (if that succeeded) rename the temporary file to the name of the input file.
sed "1s%.*%baz%" test.txt > $$ && mv $$ test.txt
$$ is the process ID of the current shell; it's a convenient way to get a reasonably unique file name. Or you could use, say,
test.txt.$$, or even
tmp if you don't have something with that name.
In the particular case of the
sed command, you can use the
-i option to modify a file in place -- but that just causes
sed to create a temporary file internally. The syntax, or even the existence, if the
-i option can vary from one
sed implementation to another; check your local documentation.