1

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.

  • 1
    You need quotes "$(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
  • @steeldriver Thank you! That did the job - I thought I had tried that, but I had only tried them at other places apparently :) Do you mind writing an answer that I can accept? – creativecreatorormaybenot Jul 01 '20 at 02:35

0 Answers0