When I use the following command in Bash, it works fine:
sed -n '/##/{p; :loop n; /##/q; p; b loop}' FILENAME.txt
It outputs something like this:
##
- Some line
Command substitution
However, when I try to use the same command in a command substitution, it suddenly fails completely:
echo $(sed -n '/##/{p; :loop n; /##/q; p; b loop}' FILENAME.txt)
This instead prints out all of the files in my working directory in a row and then Some line
at the end, which I have no clue how that would even happen:
FILENAME.txt OTHER_FILE.exe dir Some line
How can I make this work? It is crucial for me that the output of sed
is echoed with some prefix.
"$(sed -n '/##/{p; :loop n; /##/q; p; b loop}' FILENAME.txt)"
to prevent word-splitting and pathname expansion – steeldriver Jul 01 '20 at 02:27