51

I want to replace #Banner none with Banner /etc/sshd_banner that is within /etc/sshd_config. If I run

sudo sed -i "s/#Banner none/Banner \/etc\/sshd_banner" /etc/sshd_config

I get the following error

sed: 1: "/etc/sshd_config": unterminated substitute pattern

Any ideas on how to fix this issue?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Technic1an
  • 2,697
  • 5
  • 20
  • 22
  • 2
    Add a terminating / to the substitute expression. Ala `"s/#Banner none/Banner \/etc\/sshd_banner/"` – Brian Tiffin Feb 18 '15 at 19:23
  • That did not seem to work. – Technic1an Feb 18 '15 at 19:24
  • 1
    ? Meaning...? The substitute didn't work, or the error message didn't go away? One of the neat things about sed is that / is just a convention for the delimiter. Try `sed "s|#Banner none|Banner /etc/sshd_banner|" infile` to simplify the readability. – Brian Tiffin Feb 18 '15 at 19:28

3 Answers3

79

Three problems with your command:

  1. You're missing the terminating /.
  2. You can't use / as delimiter anyway, because this character occurs in the string you're trying to replace/substitute. You should use a different character, such as a pipe character, as delimiter.
  3. In the version (BSD) of sed that ships with Mac OS X, the -i flag expects a mandatory <extension> argument, which your command is missing. An empty string ("") should follow the -i flag if you want to edit the file in-place with this version of sed.

In summary, try

sudo sed -i "" "s|#Banner none|Banner /etc/sshd_banner|" /etc/sshd_config
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 38
    part 3 of this answer was super helpful, i could not find this information elsewhere while googling - OS X's version of sed is May 2005 BSD version, syntax is different in this case from GNU sed – phoenixdown Jan 17 '16 at 16:47
  • @phoenixdown in general prefer `man` over Googling because point 3 is explicitly mentioned in the manual for sed which you can find by doing `man sed` or more pedantically `man 1 sed`. – tsujp Jul 20 '22 at 13:05
55

Use another delimiter

Eks here I do use " as delimiter

sudo sed -i "" "s|#Banner none|Banner /etc/sshd_banner|" /etc/sshd_config

By changing the delimiter, you do not need to escape the /

Your original post missed one / at the end.

From OS X manual

-i extension
         Edit files in-place, saving backups with the specified extension.  If a zero-length extension
         is given, no backup will be saved.  It is not recommended to give a zero-length extension when
         in-place editing files, as you risk corruption or partial content in situations where disk
         space is exhausted, etc.

zero-length = ""

Jotne
  • 40,548
  • 12
  • 51
  • 55
0

Try single quotes. Also, some Seds require that you escape the first delimiter if you want to use something other than /.

sudo sed -i 's\:#Banner none:Banner :etc/sshd_banner:' /etc/sshd_config

One more thing is that # is possibly being interpreted as a comment that goes on up to the end of the line.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Fernando Basso
  • 688
  • 1
  • 11
  • 25