In bash, what is the difference between <, << and <<?

Asked

Viewed 666 times

13

I’m learning now to mess with bash (Ubuntu terminal) and would like to understand the difference between these three operators.

I think that < It’s file-related, but I can’t explain what he does descriptively. I think it is to send the file value to the command to be executed.

Example:

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

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

Example:

cat <<
>
>

And the <<< seemed to me 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 they called?

3 answers

7


< Incoming redirect

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

nc -l 8888 < /etc/fstab

<< String input redirection

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

cat << FIM
bla bla
bla
FIM

Typically used to print messages on screen with indentation.

<<<

I couldn’t find a definition for this, but I use it as a redirect from the output of a command, as if it were a pipe inverted. Example:

grep -i label <<< cat /etc/fstab
  • 1

    This example of << reminds me a lot of the HereDoc Syntax of PHP

  • @Wallacemaxters in bash has the "Here Document" (<<EOF) also, PHP probably "inspired" this part as well as other things besides C, I don’t know the history of decisions behind PHP :), I’m just guessing. PS: the <<< is called "Here String", as well as the << is called "Here doc" and "Tab Suppression" (<<-, hyphen) if I’m not mistaken, trim spacing at the 'beginning of lines'. Then I’ll test, because these are things I used very little just to make sure I didn’t confuse something.

5

Basically:

< - Input operator, the previous command receives what is passed after the '<';

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

You stop the exits, too > - Exit operator, the output will be directed to whatever is in front of the '>' Note that, under Linux, everything is a file, so if you want the output of a command to be directed directly to an auxiliary screen, you can use this command, see that, just one example.

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

And the other operator, <<<, I didn’t see much use, but it would be used to read string entries.

  • I think we just missed the <<<

  • Actually I inverted there, but I’ll edit it. But see that the @Dherik manager above clarifies all the points.

  • @Celsomarigojr, put some more information of these operators and even with an example of each. There your answer gets even better. I personally like something more succinct, but I don’t know what Wallace expects :)

  • 1

    @Dherik, I’ll post some examples!

3

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

    $ xargs ls <<FIN

    /tmp

    FIN

  • <<< is called "here strings", expands the expression that follows the operator and sends it to the stdin of the command

Note: the example of the operator <<< given by @cemdorst is not good, it works because only the expression immediately after the operator (in the case of the example, the cat) is passed as stdin of the grep, the name /etc/fstab is passed as a normal argument for the grep.

There are some variations, their documentation is well described in manpage bash (look up "Here Documents", "Here Strings").

Basically their utility is to pass text to the stdin of other commands, since this (text processing received via stdin) is very much based on shellscript programming. Without "here Documents" for example, it would be necessary to use commands echo more syrups, or create extra external files other than the script you are writing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.