According to

suppress automatic printing of pattern space

I've tested with or without -n, sed will produce same result

I dont understand what space does it means.

up vote 9 down vote accepted

Just try a sed do-nothing:

sed '' file


sed -n '' file

First will print whole file but second will NOT print anything.

Sed has two places to store text: pattern space and hold space. Pattern space is where each line is put to be processed by sed commands; hold space is an auxiliary place to put some text you may want to use later. You probably will use only pattern space.

Before sed goes to process a line, it is put in the pattern space. Then, sed applies all commands (such as s///) to de pattern space and, by default, prints the resulted text from the pattern space. Let us suppose we have a file myfile with a line like:

The quick brown fox jumps over the lazy dog.

We run the following command:

sed 's/fox/coati/;s/dog/dingo/' myfile

Sed will apply s/fox/coati/ and then s/dog/dingo/ for each line and in some moment will get the cited line. When it occurs, it will put the line in the pattern space, which will have the following content:

The quick brown fox jumps over the lazy dog.

Then, sed will run the first command. After sed runs the command s/fox/coati/, the content of the pattern space will be:

The quick brown coati jumps over the lazy dog.

Then sed will apply the second command, which is s/dog/dingo/. After that, the content of the pattern space will be:

The quick brown coati jumps over the lazy dingo.

Note that this only happens in memory - nothing is printed by now.

After all commands have been applied to the current line, by default, sed will then get the content of the pattern space and print it to the screen. However, when you give -n as an option to sed, you are asking sed to do not execute this last step except if it is explicit required. So, if you run

sed -n 's/fox/coati/;s/dog/dingo/' myfile

nothing will be printed.

But how could you explicitly request sed to print the pattern space? Well, you can use the p command. When sed finds this command, it will print the content of the pattern space immediately. For example, in the command below we request sed to print the content of the pattern space just after the first command:

sed -n 's/fox/coat/;p;s/dog/dingo/' myfile

The result will be

$ sed -n 's/fox/coati/;p;s/dog/dingo/' myfile 
The quick brown coati jumps over the lazy dog.

Note that only fox is replaced. It happens because the second command were not executed before the pattern space being printed. If we want to print the pattern space after both commands, we just put p after the second one:

sed -n 's/fox/coati/;s/dog/dingo/;p' myfile

Just a note: you can pass the p flag to the s/// command too:

sed -n 's/fox/coati/;s/dog/dingo/p' myfile 

It is a bit unusual AFAIK but has the advantage that the line will be print only the flagged replacement was executed. It may be very useful sometimes.


  • 1
    +1 for a very detailed answer – Lee Netherton May 4 '11 at 16:40

This puts sed into quiet mode, where sed will suppress all output except for when explicitly stated by a p command:

          By default, sed will print out the pattern space at
          the  end  of  each cycle through the script.  These
          options disable this automatic  printing,  and  sed
          will  only  produce  output when explicitly told to
          via the p command.

An example of this would be if you wanted to use sed to simulate the actions of grep:

$echo -e "a\nb\nc" | sed -n '/[ab]/ p'

without the -n you would get an occurrence of c (and two occurrences of a and b)

$ echo "a b c d" | sed "s/a/apple/"
apple b c d

The pattern space is printed implicitly.

$ echo "a b c d" | sed -n "s/a/apple/"

No output.

$ echo "a b c d" | sed -n "s/a/apple/p"
apple b c d

Explicitly print the pattern space.

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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