Difference between revisions of "Heredoc"

From EditPlus Wiki
Jump to: navigation, search
(Bash Syntax)
Line 19: Line 19:
 
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.
 
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 alternative looks like this:''
+
''The here document looks like this:''
  
 
  #!/bin/bash
 
  #!/bin/bash
Line 27: Line 27:
 
  ...
 
  ...
 
  LimitString
 
  LimitString
 
  
 
==External Links==
 
==External Links==

Revision as of 08:49, 24 August 2005

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

External Links

PHP Heredoc Reference

Advanced Bash-Scripting Guide: Chapter 17. Here Documents