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.
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:
echo dump | echo two
to get two dump
in output, but is only two
. Why?
– andrej
Mar 15 '16 at 08:21
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
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
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
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
: printf '%s %s\n' dump two
– Wildcard
Mar 15 '16 at 08:48
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
|
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.
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
echo
takes its input from the command line, not from a pipe. – cas Mar 15 '16 at 11:16