||
and |
as you show them are unix shell operators. They may look similar, but ||
and |
are totally different.
|
is the pipe operator. It connects the standard output of the left command to the standard input of the right command.
By default standard output of each command is the terminal screen.
% echo -e "a\nb\nc"
a
b
c
But the |
operator lets us redirect that standard output to the standard input of the next command:
% echo -e "a\nb\nc" | grep a
a
The shell handles opening up a pipe and connecting its output and input file descriptors to the left and right commands respectively. The actual commands thus don't generally have to know or care whether their output is a terminal, a file, another program behind a pipe.
||
is the logical OR operator. It runs the second command if the first command does not return success exit code (ie 0
).
% test -f /usr/local/bin || echo "/usr/local/bin is not a file"
/usr/local/bin is not a file
% test -f /bin/bash || echo "/bin/bash is not a file"
There is also a logical and operator that does the opposite:
% test -f /bin/bash && echo "/bin/bash is a file"
/bin/bash is a file
And that in turn should not be confused with the &
operator, which puts the command in the background.
% sleep 3 &
[1] 55981
% jobs
[1] + running sleep 3
% fg
[1] + running sleep 3
%
In general, shell syntax can be rather esoteric, and you should be aware that every shell is a little different (eg c/ bourne / korn shells vs bash vs zsh, the latter being now the standard on Mac Os)
This is all completely distinct from the command line arguments you access via argv
and argc
. If you wanted the standard output of one command to be converted to arguments to the second command, xargs
can do it.