0

Consider the following command:

echo ’.dump’ | sqlite bacula.db > bacula.sq

What is it doing and what does | do?

Maybe someone could point me to a manual about piping, or explain what is going on. Thanks.

Alexander
  • 9,607
  • 3
  • 40
  • 59
andrej
  • 129
  • 1
  • 5
  • You might find [this description of the shell at a conceptual level](http://unix.stackexchange.com/a/169765/135943) to be very helpful; I certainly did. – Wildcard Mar 15 '16 at 09:50
  • It doesn't say that because it's not relevant - echo 'ignores' pipe input in the same way that human eyes 'ignore' ultra-violet or infra-red, or the way that a defenceless vegemite sandwich 'ignores' the fact that it is approaching a hungry mouth - they have no capacity to perceive it. `echo` takes its input from the command line, not from a pipe. – cas Mar 15 '16 at 11:16

3 Answers3

1

Shell pipe operator | makes standard output for a command the standard input for the next command, without creating an intermediate file.

You can find detailed information explained in a simple way in the following sources:

assefamaru
  • 804
  • 6
  • 6
  • I would expect from `echo dump | echo two` to get `two dump` in output, but is only `two`. Why? – andrej Mar 15 '16 at 08:21
  • 3
    @andrej that's because `echo` doesn't act on its input; so `dump` is fed into the second `echo`'s input, which ignores it and outputs `two`. – Stephen Kitt Mar 15 '16 at 08:34
  • This is because `echo` ignores standard input, and dumps command line arguments to standard output. In your example, `dump` is sent to standard input of the second `echo`, but gets ignored, hence only `two` gets printed. To avoid this, you can use `xargs` to make the second echo read from its std input. – assefamaru Mar 15 '16 at 08:35
  • To illustrate with an example, `echo dump | echo` prints an empty line. `echo dump | echo two` prints "two". `echo dump | cat` prints "dump". `echo dump | xargs echo two` prints "dump two". – assefamaru Mar 15 '16 at 08:37
  • 1
    Teaching `xargs` this way is a really really bad idea. There are cases where `xargs` should be used but it's *not* a beginner command, because there are too many security issues that can be created that way. If you want to run both commands, just run both commands. `echo dump; echo two` If you don't want the first newline suppress the first newline. `echo -n dump; echo two` Or just use one command: `echo dump two` Or do it fancy and [use `printf`](http://unix.stackexchange.com/q/65803/135943): `printf '%s %s\n' dump two` – Wildcard Mar 15 '16 at 08:48
  • I was looking in `man echo` for info about ignoring input from pipe. I couldn't find information about it. Where can I found info about such exception, what other commands are ignoring inputs like echo? – andrej Mar 15 '16 at 09:39
  • 1
    @andrej, it doesn't say it ignores the brightness sensor on your laptop, either, but it does. The command does what is described in the man page—nothing more, nothing less. If it doesn't say in the man page that it does something with the standard input then you shouldn't expect it to. That applies to any command and any man page; they're not "exceptions." – Wildcard Mar 15 '16 at 09:43
1

| is a pipeline operator in Unix/Linux. It could be used where the output of the first command can be used as input to the second command.

For example:

ls -l | less will show the longlist of your files in the directory. The less command takes the output of ls -l as input and displays the list of files where you scroll up/down and see them.

Archemar
  • 31,183
  • 18
  • 69
  • 104
1

This command writes the string consisting of the seven characters ’.dump’ followed by a newline character to the sqlite command. (That's 12 bytes in all.)

The sqlite command will fail to understand the instruction and will so write nothing to the target file bacula.sq, reporting Error: incomplete SQL: ’.dump’ to stderr.

Perhaps you meant this instead, which uses single quote characters ' instead of apostrophe marks :

echo '.dump' | sqlite bacula.db > bacula.sq
roaima
  • 107,089
  • 14
  • 139
  • 261