# What is the purpose of -e in sed command?

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

e.g.

sed 's/foo/bar/'


VS

sed -e 's/foo/bar/'

-

## migrated from stackoverflow.comMar 1 '12 at 17:30

This question came from our site for professional and enthusiast programmers.

Are you sure you didn't find this in man sed? –  BoltClock Mar 1 '12 at 8:32
You don't find potongs explanation, why not to use "cmd1;cmd2" –  user unknown Mar 1 '12 at 22:51

This might work for you:

sed -e '/foo/i\bar' -e '/fred/a\barny' -e '/harry/c\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 good tip for those commands. –  glenn jackman Mar 1 '12 at 14:41
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 Oct 16 '12 at 7:35

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.

-
-e script
--expression=script


Add the commands in script to the set of commands to be run while processing the input.

-