I have a simple script which runs on a FreeBSD machine with the following code:

`sed -i .bak '\:#start,\:#end' /usr/local/etc/racoon/racoon.conf`
echo $?

It should delete a block of text between the two patterns.

The problem is that if I run the sed command directly from shell it works, if i run the script the return code is 0.

Why's that?

  • the command is sed -i .bak '\:#start,\:#end' /usr/local/etc/racoon/racoon.conf, between `` so it can be executed within the shell script Jun 18, 2010 at 13:10
  • From the phrasing of your question, it seems like you weren't expecting a return value of 0 for a successfully executed command, when this is precisely the behavior a command should exhibit.
    – Tekhne
    Jun 18, 2010 at 18:22

2 Answers 2


You need to eliminate the backticks and change the single quotes to double quotes:

echo $ip
sed -i .bak "\:#start ${ip}:,\:#end ${ip}:d" /usr/local/etc/racoon/racoon.conf

Using double quotes will allow the variables to be expanded. The backticks would try to execute the standard output of the command (which there is none in this case). For example, if you did `echo hi` it would try to execute "hi" as a command. sed always returns 0 unless there's an error regardless of whether a replacement was made.

  • thanks!worked. however if we are here i want to ask you something else (since this was a sub-problem of my big problem). I have a script called rootexec.sh which contains the code : #!/bin/sh exec $1 (this script is used as a parameter for a sudo). The problem is that if i use script.sh 'sed -i .bak "\:#start,\:#end" /usr/local/etc/racoon/racoon.conf' (the sed being the $1 parameter for the exec command), i get this error : sed: 1: ""\:#start": invalid command code " . exec seems to have a problem with it? Jun 18, 2010 at 13:54
  • should i post this as a separate problem? Jun 18, 2010 at 14:41
  • @Darie: There's a serious security risk of passing commands as parameters to scripts that get run using sudo. That said, you might try putting double quotes round $1: exec "$1". Also read BashFAQ/050. Jun 18, 2010 at 14:51
  • with "$1" i get this error : exec: sed -i .bak "\:#start,\:#end" /usr/local/etc/racoon/racoon.conf: not found I am working on a system build by somebody else and i need to use their scripts. Jun 18, 2010 at 15:15
  • @Darie: The next thing I would suggest trying is, instead of the double quotes, escape the backslashes or use forward slashes: script.sh 'sed -i .bak "\\:#start,\\:#end" /usr/local/etc/racoon/racoon.conf' or script.sh 'sed -i .bak "/#start,/#end" /usr/local/etc/racoon/racoon.conf' Jun 18, 2010 at 15:48

The backticks really are not necessary (or perhaps I understand you wrong). The fact that the script returns 0 is also expected: exit code 0 is OK. Edit: sed will always exit with 0, even if there has been no substitution. Another exit code is only then generated when there is an error in your syntax.

More interesting is: is the code deleted or not?

Judging from your comments, you probably have a problem with regexp and / or escaping characters in it.

  • actually the script is using a parameter (the ip) #!/bin/sh ip=$1 echo $ip sed -i .bak '\:#start ${ip}:,\:#end ${ip}:d' /usr/local/etc/racoon/racoon.conf echo $? The text block doesnt get deleted from the file even if the return code is OK. Jun 18, 2010 at 13:16
  • the .bak file is created (that means the command was executed, however the block of code was not removed) Jun 18, 2010 at 13:19

You must log in to answer this question.

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