In bash, what is the difference between, and?

11

I'm now learning to tinker with bash (Ubuntu Terminal) and would like to understand the difference between these three operators.

I think < is related to files, but I can not explain what it does descriptively. I think it's to send the value of the file to the command to execute.

Example:

cat < index.php
#Exibe o conteúdo do arquivo

When I use << , it opens a new line, without executing the previous command (I do not understand what it does right).

Example:

cat <<
>
>

And <<< seemed to be related to expressions.

Example:

cat <<< "O número é $((13 + 2))"
O número é 15
  • So, in which case do I use < , << , or <<< ?

  • What is the purpose of each?

  • What are their names?

asked by anonymous 26.08.2015 / 18:35

3 answers

6

< input redirection: For commands waiting for an input (usually a keyboard), you can redirect a file to the input (just like you did with cat < index.php ). Another example:

nc -l 8888 < /etc/fstab

<< string entry redirect: same thing, but instead of passing a file, you directly type the string (in multiple lines). You end the entry with a CTRL+D , or as follows:

cat << FIM
bla bla
bla
FIM

Usually used to print on-screen messages with indentation.

<<< I could not find a definition for this, but I use it as a redirector for the output of a command, as if it were an inverted pipe . Example:

grep -i label <<< cat /etc/fstab
    
26.08.2015 / 19:18
5

Basically:

< - Input operator, the preceding command receives whatever is passed after '

<< - Input operator, with append, you can use for more than one input line. I do not see much use;

Stop the outputs idem > - Output operator, output will be directed to whatever is in front of '>' Note that in Linux, everything is a file, so if you want the output of a command to go directly to an auxiliary screen, you can use this command, see #

>> - Output with append means that if the output is to a file, it will be incremented with the current output.

The other operator,

26.08.2015 / 18:54
3
  • < is the input redirector, sends a file to the standard input (stdin) of the command
  • << is called "here documents", it is usually used to write an inline string in the script and send it to the command stdin. The string is terminated by the token that follows the < < appearing alone at the beginning of a line:

    $ xargs ls

22.09.2016 / 19:35