I can't find any documentation about the sed -e switch, for simple replace, do I need it?


sed 's/foo/bar/'


sed -e 's/foo/bar/'
  • 16
    Are you sure you didn't find this in man sed?
    – BoltClock
    Commented Mar 1, 2012 at 8:32
  • 4
    You don't find potongs explanation, why not to use "cmd1;cmd2" Commented Mar 1, 2012 at 22:51
  • @BoltClock, I think they should check the texinfo instead, to know what are "sed scripts" since the the use of word "script" in the manual page is confusing with its usual use.
    – Kornee
    Commented Oct 21, 2022 at 17:02

4 Answers 4


From the man page:

-e script, --expression=script

    add the script to the commands to be executed

So you can use multiple -e options to build up a script out of many parts.

$ sed -e "s/foo/bar/" -e "/FOO/d"

Would first replace foo with bar and then delete every line containing FOO.

  • 21
    So, you don't need '-e' if you only have one script, right?
    – JohnyTex
    Commented Aug 8, 2017 at 10:26

This might work for you:

sed -e '/foo/i\' -e 'bar' -e '/fred/a\' -e 'barny' -e '/harry/c\' -e 'potter' file

In each case the i(insert),a(append) and c(change) commands need to be terminated by a newline.

Normally commands can be separated by a ; e.g. /foo/d;/bar/d and grouped by {...} e.g. /foo/{h;d} but for the i,a,c commands the -e provides a way of separating the commands.

The alternative is to use the shell(bash) to insert a newline:

sed '/foo/i\bar'$'\n''/fred/a\barney'$'\n''/harry/c\potter' file
  • 1
    On at least some sed implementations (for instance, on FreeBSD), you also need to end labels and branch commands using newlines/-e, rather than with semicolons. This won't work: sed ':a; /x/ { s/x/y/g; ba }; q' <<< "jxm". But this will: sed -e ':a' -e '/x/ { s/x/y/g; ba' -e '}; q' <<< "jxm"
    – dubiousjim
    Commented Oct 16, 2012 at 7:35
  • I'm not sure how necessary is the must be terminated with a newline phrase means here. When I tested here in my cygwin, this works: sed '/foo/i\bar' file. So now that we can fuse together the separate expressions into one, this raises again to the original question: What is the true purpose of the -e ?
    – daparic
    Commented Oct 22, 2018 at 17:11

In the help for sed you will find that -e flag stands for:

 -e script, --expression=script
              add the script to the commands to be executed

You are not the first to be confused after encountering -e. In your example sed 's/foo/bar/' and sed -e 's/foo/bar/' are equivalent. In both cases s/foo/bar/ is the script that is executed by sed. The second option is more explicit, but that is probably not the reason that you often see -e used. The reason for that is that -e makes it possible to use more than one script with the same invocation of sed. That still leaves the question why use it if you are using only one script? The following example might explain that:

echo "foo" | sed -e 's/foo/bar/' -e 's/bar/foo/'

Will output "foo".


echo "foo" | sed 's/foo/bar/' -e 's/bar/foo/'

will fail because now sed interprets the first s/foo/bar/ as a filename. Hence, to make life easier for future you, you can already start using the -e flag today. That way future you can just append the command with an extra script by using -e <script>.

N.B. In other answers using ; as an alternative to use multiple scripts has been suggested. While that is technically possible in some cases, it is not encouraged, because it quickly leads to very hard to read code. See an example here: https://www.grymoire.com/Unix/Sed.html#uh-61


-e, equivalent to --expression, is optional, unless you are stringing several expressions together (not common) as shown in another Answer.

Aside: ; can be used instead in the same expression to execute several expressions one after the other.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .