Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
the command is sed -i .bak '\:#start,\:#end' /usr/local/etc/racoon/racoon.conf, between `` so it can be executed within the shell script – Darie Nicolae Jun 18 '10 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 '10 at 18:22
up vote 7 down vote accepted

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.

share|improve this answer
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 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 '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? – Darie Nicolae Jun 18 '10 at 13:54
should i post this as a separate problem? – Darie Nicolae Jun 18 '10 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. – Dennis Williamson Jun 18 '10 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. – Darie Nicolae Jun 18 '10 at 15:15
@Darie: The next thing I would suggest trying is, instead of the double quotes, escape the backslashes or use forward slashes: 'sed -i .bak "\\:#start,\\:#end" /usr/local/etc/racoon/racoon.conf' or 'sed -i .bak "/#start,/#end" /usr/local/etc/racoon/racoon.conf' – Dennis Williamson Jun 18 '10 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.

share|improve this answer
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. – Darie Nicolae Jun 18 '10 at 13:16
the .bak file is created (that means the command was executed, however the block of code was not removed) – Darie Nicolae Jun 18 '10 at 13:19

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.