2

Apparently < and | behave similarly.

cat file | wc
wc < file

Is there a name for this '<' symbol when used in this way, so that one could find its documentation?

man <
man '<'

do not reveal anything; and in a Web search, the symbol is ambiguous, since it is used mathematically and as a line prefix in emails. Similarly, the

if [ -r file ]

construct can be restated as

if test -r file

and one can then find the manual page for the "test" utility. (Try searching for documentation on square bracket!) What name or string do I search for to find documentation on the behavior of '<'? Also '<<'?

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
Jacob Wegelin
  • 1,304
  • 11
  • 16
  • Error in 1st line of example. Should it be `cat file | wc`? – ctrl-alt-delor Oct 12 '18 at 16:54
  • 3
    For bash, it's documented in [3.6 Redirections](https://www.gnu.org/software/bash/manual/bashref.html#Redirections), but where do you get the idea that it spawns a subshell? It does not. – glenn jackman Oct 12 '18 at 16:55
  • 1
    For POSIX shells see [here](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07) – virullius Oct 12 '18 at 16:58
  • The questions about `test` already have answers in our knowledgebase, and if asked on their own, would be closed as duplicative. As it is, mixing them in with a question on redirection puts the question on "too broad" territory. – Charles Duffy Oct 12 '18 at 17:10
  • @CharlesDuffy OP **is not asking about `test`**, they are saying is it alike. That is they are asking is it like knowing that to find out about `[` one must findout about `test`. They are asking is there a similar path to finding out about `<` and `>`. – ctrl-alt-delor Oct 12 '18 at 17:18
  • I'm... not sure that claim is actually true. `help [` works just as well as `help test`. – Charles Duffy Oct 12 '18 at 17:20

2 Answers2

4

Apparently < and | behave similarly

There are slight similarities, but you can't take that too far. They both represent redirection.

< redirects the standard input stream (stdin) of the command on the left to refer to the file on the right. The command will run in its own process only if it is an external program (like wc).

| redirects the standard output stream (stdout) of the command on the left to the standard input stream (stdin) of the command on the right. The mechanism used is called an anonymous pipe, and both commands run in their own child process.

< and | are not commands, they are part of shell grammar, so there won't be any man pages for them. As part of the bash language you have to use man bash then search for REDIRECTION (for <) or Pipelines (for |).

Section 1 man pages are for programs, not one specific language like a shell, section 2 is for kernel APIs called from C, etc. So don't expect details of your specific shell to appear as separate man pages. So there is a man bash, man sh, man csh, man zsh, etc.

Surprisingly, [ is a shell builtin:

$ type [
[ is a shell builtin

and is also known as test. But:

$ type [[
[[ is a shell keyword

[, test, [[ are all commands and additional documentation is available in bash using help, e.g.

$ help [

So how do you know if the symbol is a command or part of the grammar? By learning the language, there is no other way. Follow tutorials, read man bash, look at examples, but above all experiment yourself and try things out.

It is not easy for a beginner, but you should remember that shell syntax was written for programmers to use and understand, it was never designed for end-users.

You remark (complain?) that the same character is used for different meanings. That is common within programming languages. For example, in C * is used to qualify a declaration of a pointer, as a multiplication binary operator, as half a comment symbol, and as a unary operator to dereference a pointer. There are many other multi-use characters in various languages. This is partly because early keyboards were only capable of the printable part of the 128 character ASCII set. Attempts at using Unicode operators (e.g. Perl 6) have not been widely adopted.

cdarke
  • 42,728
  • 8
  • 80
  • 84
2

>, >>, < and | are part of the shell. They are not commands.

See man bash (or manual for your shell)

| is called a pipe. It is the name of the operation, not the symbol. >, and < are called output and input redirection.

The two commands

cat file | wc
wc < file

Do different things, but have the same result (but the 2nd is more efficient).

The first creates a pipe then forks and execs twice (one for cat, one for wc).

The second redirects stdin, the forks and execs once (just for wc).

On test and []

You are correct that [ is another name for test. Unfortunately > and < are not commands. So their is no manual page for them, but they are in the manual for your shell. The key-words you need to know are file redirection, input redirection, output redirection.

References

https://en.wikipedia.org/wiki/Redirection_(computing)

Community
  • 1
  • 1
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52