2

I was trying to install docker in Ubuntu, and following the instructions I have come across a very strange sed command which I do not understand (this seems to be used to set-up bash autocompletion for docker):

sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

What is that command doing? The -i command means in-place editing, but what does the $acomplete -F _docker docker mean? The $ is matching the last line, but what is it doing? I do not even recognize any sed command there! For example, a substitution command would look like:

$s/in/out/

Could somebody explain that expression for me?

blueFast
  • 41,341
  • 63
  • 198
  • 344

1 Answers1

5

It appends complete -F _docker docker to the file.

From sed's manual:

a \

text Append text, which has each embedded newline preceded by a backslash.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • looks strange, I think, why not `echo >>/path/to/file` – Dmitry Ginzburg Jul 28 '14 at 08:48
  • What trick of `sed` is using to do that? Could you point me to the relevant part of the documentation? – blueFast Jul 28 '14 at 08:48
  • @jeckyll2hide I think the advantage of it is that whether the file ends with a newline or not it would still append a new line to it. So it's certain that `complete ...` would be on a new line. – konsolebox Jul 28 '14 at 08:49
  • 1
    @DmitryGinzburg: it can not use redirection because of permissions (you can not send output to a file owned by root) – blueFast Jul 28 '14 at 08:50
  • Do `echo a > b; sed -i ab b` or do `echo -n a > b; sed -i ab b`, `b` would still have `a\n\b\n`. – konsolebox Jul 28 '14 at 08:53
  • 1
    @DmitryGinzburg: sure, starting sudo, and a new bash. Running just sed seems faster. The syntax is a bit weird, until you know what it is for. I am all for the sed solution now. – blueFast Jul 28 '14 at 08:54
  • @jeckyll2hide I think, overhead on running new `sh` (not bash) is not so high, but the readability of `sed` usage here (which, I think, also would be slower a little bit, than running new `sh`) is much poor. – Dmitry Ginzburg Jul 28 '14 at 08:58
  • @jeckyll2hide I'm with you, it isn't that bad to use sed, as it is indeed an editor. However, the shell based solution which (at)Dmitry favorites is good as well. Maybe the developers should just add a comment above that line which explains what they are doing and why. – hek2mgl Jul 28 '14 at 08:59
  • Normay `sed 'a...` need a `\ ` backslash (depending OS/shell a space between a and backslash) followed by a new line than the wanted text or in this case there is nothing. Is it a specific behaviour of GNU sed (on my AIX, it doesn't work) ? – NeronLeVelu Jul 28 '14 at 09:22
  • @jeckyll2hide how is sed managing to overwrite a file that you can't append to? `sed -i` just makes a tmp file and then overwrites the original with that, it's not actually editing the original file. – Ed Morton Jul 28 '14 at 21:00
  • @EdMorton: it is run with `sudo` so it can do anything – blueFast Jul 28 '14 at 21:10