2

I am looking for an example of << redirection because i don't understand it well. I know < is for sending the contents of specified file to be use as standard input like :

cat < file.txt

but i don't understand what << does. i saw a description about it that says "accept text of the following line as standard input" but still confused.

Archemar
  • 31,554
amir jj
  • 1,212

3 Answers3

3

This allows you to embed the text that will be fed to a command within a script in the script itself instead of an external file. So, instead of redirecting from another file, you could have the input content in a single file containing both the script and the input data.

This is specially useful when mixing shell scripts and awk, perl or other scripting languages.

This is important to notice that the word after the << redirection is considered the end of the redirection.

A simple example:

#!/bin/sh
cat << EOF > output.txt
line 1
line 2
line 3
EOF
echo done

This will produce a file output.txt containing:

line 1
line 2
line 3

and the scrip will just print done to the terminal

Marcelo
  • 3,561
1

this is call a here document.

This is mainly use if you want to define a portion of a file in a command shell. I.e.

cat <<foobarbaz > /tmp/file.txt
hello 
world
$$
foobarbaz

will populate file /tmp/file.txt with

hello
world
1234

assuming 1234 is process ID.

Archemar
  • 31,554
0

This operator implements a here document, which takes text from the following lines as standard input.Chances are you won’t use this redirector on the command line, though, because the following lines are standard input, so there’s no need to redirect them.Rather, you might use this command as part of a script in order to pass data to a command.Unlike most redirection operators, the text immediately following the << code isn’t a filename;instead, it’s a word that’s used to mark the end of input.For instance, typing someprog << EOF causes someprog to accept input until it sees a line that contains only the string EOF (without even a space following it).

Note: Some programs that take input from the command line expect you to terminate input by pressing Ctrl+D. This keystroke corresponds to an end-of-file marker using the American Standard Code for Information Interchange (ASCII).

amir jj
  • 1,212