I am trying to insert a line into a coffee script using sed and it looks as if it is ignoring my first tab character.

The file I want to insert into has a line like this:


and I want to insert a line after it like this:

        flags: ['--no-sandbox']

So I wrote a sed script that I think should work

sed "/^\t\tkarmaServer.start/a\t\t\t\tflags: ['--no-sandbox']" build/unit-test.coffee 

although the result is quite odd. I get the following

t       flags: ['--no-sandbox']

And I don't understand why it starts the line with a t character or how to get rid of it.

any ideas?

  • 1
    This is not the behaviour of all seds, note. Spinellis sed says "extra characters after \ at the end of a command" and fails.
    – JdeBP
    Feb 27, 2019 at 15:54

1 Answer 1


You must add a newline in your sed command (and then account for the shell's handling of escaped newlines):

 sed "/^\t\tkarmaServer.start/a\\
 \t\t\t\tflags: ['--no-sandbox']" build/unit-test.coffee 

As the GNU sed manual says:

a \

text Append text, which has each embedded newline preceded by a backslash.

This is not very clear, unformatted like this. (It isn't particularly clearer when viewed with the man command, alas.) What it is telling you is that the format of the a command includes a backslash, a newline, and then an argument denoted by text. Everything in that column on the left, in other words.

As noted in question comments, seds differ in how they handle malformed commands. Spinellis sed yields an error, telling you that you have something other than the required backslash followed by newline:

% sed "/^\t\tkarmaServer.start/a\t\t\t\tflags: ['--no-sandbox']" /dev/null
sed: 1: "/^\t\tkarmaServer.start ...": extra characters after \ at the end of a command

Your particular choice of sed appears to be simply treating the rest of the line as the text argument. But the first backslash there is still counting as the mandatory backslash that should follow a. Therefore, what you end up with as the text argument is

t\t\t\tflags: ['--no-sandbox']

Notice the initial t, and one fewer tabs than you expected.

  • Right part of the manual. Wrong actual answer.
    – JdeBP
    Feb 27, 2019 at 15:56
  • @JdeBP is right... I needed to prepend \ not only a ``
    – munHunger
    Feb 27, 2019 at 15:58
  • 1
    Actually I need a few more backslashes on my machine: sed "/^\t\tkarmaServer.start/a\\\\t\\t\\t\\tflags: ['--no-sandbox']" build/unit-test.coffee
    – Freddy
    Feb 27, 2019 at 16:02
  • There you go. That's what the manual is actually telling you.
    – JdeBP
    Feb 27, 2019 at 16:11
  • @JdeBP Oh, wrong. Now mine works with one less... Got it, thanks!
    – Freddy
    Feb 27, 2019 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.