0

I want to comment out a line (add a # to the beginning). I used this command: sed -i '96s/^/#&/' file.txt on Linux to insert comment at line # 96.

If the line number is a variable, then how can I use this command? I have tried these commands:

(1)

sed -i '${line_number}s|^|#&|' file.txt

(2)

sed -i '${line_number}s/^/#&/' file.txt

But they don't work. How can I do this?

terdon
  • 242,166

1 Answers1

0

Variables are not expanded when using single quotes. Use double quotes instead:

 sed -i "${line_number}"'s/^/#/' file.txt

You don't need the &. It means insert whole match, which is empty.

pLumo
  • 22,565