Differences between revisions 1 and 9 (spanning 8 versions)
Revision 1 as of 2009-12-08 21:29:23
Size: 1534
Editor: GreyCat
Comment:
Revision 9 as of 2020-04-24 01:41:53
Size: 2213
Editor: unn-212-102-36-1
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
== Here Document ==
Line 6: Line 8:
someprogram <<WORD somecommand <<WORD
Line 13: Line 15:
Here, `someprogram` can be any program that reads from standard input (`cat` is by far the most common), and `WORD` can be any delimiter word you like. (`EOF` is a common choice.) Here, `somecommand` can be any program that reads from standard input (`cat` is by far the most common), and `WORD` can be any delimiter word you like. (`EOF` is a common choice.)
Line 23: Line 25:
someprogram <<'WORD' somecommand <<'WORD'
Line 30: Line 32:
If we want to be able to indent the here document, we can prefix the delimiter word with a `-` (hyphen): If we want for the body of the here document to be indented like the rest of the script while also having bash remove all leading tab characters during execution, we can add a `-` (hyphen) suffix to the immediate right of the '<<'. An empty space to the left of WORD is optional.
Line 35: Line 37:
 someprogram <<-WORD
 this is
 an indented
 here document
 somecommand <<- WORD
  this is
   an indented
    here document
Line 48: Line 50:
== Here Strings ==

In bash, there is a variant of the here document called the ''here string''. It's more compact, but also more limited:

{{{
read -a octets <<< "$ipaddr"
}}}

The `<<<` serves a role similar to that of the `<<` in a here document, but there is no sentinel word to tell us where the input ends. Rather, the `<<<` is followed by a single word ([[Quotes]] are your friend!). That word, plus a newline, become the standard input of the command.

Here Document

A "here document" is a Bourne shell syntactic feature that allows you to feed data to a program without storing it in an external file. It works equally well in POSIX, Korn and Bash shells too.

The basic form is:

somecommand <<WORD
your data
go
here
WORD

Here, somecommand can be any program that reads from standard input (cat is by far the most common), and WORD can be any delimiter word you like. (EOF is a common choice.)

Here documents of this form have certain characteristics:

  • Shell substitutions (such as $variable) in the here document are performed.

  • The delimiter word must appear on a line by itself, in the first column.
  • Any whitespace in the here document is preserved, including leading whitespace.

If we want to avoid shell substitutions, we can quote the delimiter word:

somecommand <<'WORD'
your data
$go
`here`
WORD

If we want for the body of the here document to be indented like the rest of the script while also having bash remove all leading tab characters during execution, we can add a - (hyphen) suffix to the immediate right of the '<<'. An empty space to the left of WORD is optional.

if ...
    while ....
        somecommand <<- WORD
         this is
          an indented
           here document
        WORD
    done
fi

In this form, all leading tab characters (not spaces!) will be removed. There is no provision for removing leading spaces, or leading tabs-and-spaces. (Recall the syntactic restrictions of Makefiles, and you'll be OK.)

Here documents are typically implemented by creating a temporary file and redirecting standard input from this file when the program is invoked.

Here Strings

In bash, there is a variant of the here document called the here string. It's more compact, but also more limited:

read -a octets <<< "$ipaddr"

The <<< serves a role similar to that of the << in a here document, but there is no sentinel word to tell us where the input ends. Rather, the <<< is followed by a single word (Quotes are your friend!). That word, plus a newline, become the standard input of the command.


CategoryShell

HereDocument (last edited 2020-04-24 02:48:22 by unn-212-102-36-1)