Heredoc

From EditPlus Wiki
Revision as of 05:41, 16 December 2008 by WikiSysop (Talk | contribs) (removing spam, yay)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Heredoc syntax provides a means to delimit a string without using single or double quotes. This can be useful in situations with strings that contain many single or double quotes or that break across multiple lines.

EditPlus does not natively support Syntax Highlighting for heredoc strings, however you can modify an .stx file to emulate string highlighting for heredoc strings.

PHP Syntax

In PHP, <<< (three left angle brackets) is the heredoc operator, denoting the beginning of the string. This operator is followed immediately by a custom identifier, which is then used to close the heredoc string. The closing identifier must begin in the first column of the line, containing no other characters except a semicolon (;). There can be no spaces/tabs after or before the identifier or the semicolon.

The following is an example of heredoc string usage:

$str = <<<EOF
Example of string
spanning multiple lines
using heredoc syntax.
EOF;
print $str;

Bash Syntax

In Bash, a here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor. A limit string delineates (frames) the command list. The special symbol << designates the limit string. This has the effect of redirecting the output of a file into the stdin of the program or command.

The here document looks like this:

#!/bin/bash
interactive-program <<LimitString
command #1
command #2
...
LimitString

By default bash will interpolate the content of the block like it would in a double-quoted string. If you want to avoid this (for example if your here document contains a script you want to output verbatim) you can put single quotes around your limit string.

user@host> var="hello"
user@host> cat <<LimitString
user@host>  echo $var
user@host>  LimitString
echo hello
user@host> cat <<'LimitString'
user@host>  echo $var
user@host>  LimitString
echo $var


You can also use the following syntax to allow tab characters in front of each line. This is especially useful in scripting to allow indentation.

#!/bin/bash
    interactive-program <<-LimitString
        command #1
        command #2
        ...
    LimitString

Spaces have been used in this example due to the limitations of the Wiki editor, however it should be noted that this syntax only allows tab characters.


See also

FAQ: How do I make EditPlus "correctly" highlight PHP heredoc strings?

External Links