Pages

Monday, February 4, 2013

Here Document & Here String


Here Document

we are pretty much aware of the re direction in Linux .When we use

# echo 'hello world' >output
# cat <output

The first line writes "hello world" to the file "output", the second reads it back and writes it to standard output ( To Terminal)

Then there are "here" documents:

# cat <<EOF
> hello
> world
> EOF

A "here" document is essentially a temporary, nameless file that is used as input to a command, here the "cat" command.

A less commonly seen form of here document is the "here" string:
# cat <<<'hello world'

In this form the string following the "<<<" becomes the content of the "here" document.

This type of redirection tells the shell to read input from the current source (HERE) until a line containing only word (HERE) is seen. HERE word is not subjected to variable name, parameter expansion, arithmetic expansion, path name expansion, or command substitution. All of the lines read up to that point are then used as the standard input for a command. Files are processed in this manner are commonly called here documents.

Here Docs

wc -w <<EOF
> This is a test.
> Apple juice.
> 100% fruit juice and no added sugar, colour or preservative.
> EOF

Here Strings
Command <<<$word
 wc -w <<< "This is a test."