Questions tagged [heredoc]

A Here-document is a special syntax of writing literal strings in sourcecode, used by different programming languages.

A heredoc or Here-Document is a way of writing literal strings in sourcecode. Many languages use heredocs: PHP, Perl, C#...

Although syntax can differ between programming languages, most heredocs start by putting a keyword, and end when the keyword is encountered as the only element on a line (no spaces, tabs or other characters are allowed). Perl example:

my $text = <<KEYWORD; #Any non-reserved word would do
This is some text.
It can contain $othervariables which will be replaced
Unless KEYWORD was between single quotes, in Perl.
KEYWORD
761 questions
16
votes
5 answers

\n in variable in heredoc

Is there any way to for a Bash heredoc to interpret '\n\' in a heredoc? I have an iteratively built string in a loop, something like for i in word1 word2 word3 do TMP_VAR=$i ret="$ret\n$TMP_VAR" done and then I want to use the…
Kamil Roman
  • 973
  • 5
  • 15
  • 30
16
votes
3 answers

Can I read line from a heredoc in bash?

Here's what I'm trying. What I want is the last echo to say "one two three four test1..." as it loops. It's not working; read line is coming up empty. Is there something subtle here or is this just not going to work? array=( one two three ) echo…
Shawn J. Goff
  • 4,635
  • 8
  • 34
  • 38
15
votes
4 answers

need to put code comments inside a heredoc

Well I can't seem to add comments inside a heredoc block in my foo.php file: echo <<<_HEREDOC_FOO // okay this comment was intended to explain the code below but it // is showing up on the web page HTML sent to the browser …
wantTheBest
  • 1,682
  • 4
  • 43
  • 69
15
votes
4 answers

Heredoc not working

Result: Parse error: syntax error, unexpected T_SL on line 3
Enemy of the State
  • 228
  • 1
  • 2
  • 6
15
votes
1 answer

Bash: warning: here-document at line delimited by end-of-file (wanted `EOF')

The following function in bash comes up with the error mentioned in the title. The error usually appears when the final EOF is not at the beginning of the line. EOF is at the beginning so I can't see what is wrong. Further up in the script (not…
slooow
  • 329
  • 1
  • 3
  • 10
14
votes
3 answers

Is it possible to concatenate heredoc string in PHP..?

With normal PHP string you can do this: $str = "Hello "; $str .= "world"; $str .= "bla bla bla"; $str .= "bla bla bla..."; But can you do the same with heredoc string..? $str = <<
pnichols
  • 2,198
  • 4
  • 24
  • 29
14
votes
2 answers

pipe here document command to log file

Quite often I'll use the following construct to pipe output to a log file, keeping the output also on the display ./command 2>&1 | tee output.log I'm trying to do something similar, but with using a here document: ./command <<…
Trent
  • 2,328
  • 3
  • 33
  • 51
13
votes
5 answers

What is <<<_END?

I'm new to PHP and don't understand what the point of <<<_END is. Could someone please explain when this should be used? I've looked at various examples and they all seem to have HTML embedded within them. But I can use HTML without the <<<_END…
Skoder
  • 3,983
  • 11
  • 46
  • 73
13
votes
2 answers

How to suppress variable substitution in bash heredocs

Is it possible to create a heredoc that does not become subject to variable expansion? e.g. cat <<-EOF > somefile.sh Do not print current value of $1 instead evaluate it later. EOF Update I am aware of escaping by \. My actual heredoc has many…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
11
votes
3 answers

define and heredoc

How do you use define within a heredoc? For example: define('PREFIX', '/holiday'); $body = << // This doesn't work. EOD;
moey
  • 10,587
  • 25
  • 68
  • 112
11
votes
1 answer

In C++, calling fork when cin is a bash heredoc causes repeated input fragments

I am implementing a shell-like program in C++. It has a loop that reads from cin, forks, and waits for the child. This works fine if the input is interactive or if it's piped from another program. However, when the input is a bash heredoc, the…
Kevin Chen
  • 994
  • 8
  • 24
11
votes
1 answer

Running script with HERE_DOC method in background

I have a script that should be run in background. I must answer a question as soon as I run the bash..How can I do that? (nohup python script.py lst '<
MLSC
  • 5,872
  • 8
  • 55
  • 89
11
votes
1 answer

PyCharm autocomplete, list of types

How I can tell with Docstring to PyCharm that return type is the list of SomeClass instances? Tried out: @rtype [SomeClass], but it acts only as list.
fedosov
  • 1,989
  • 15
  • 26
10
votes
3 answers

What is the best way to have long string literals in Javascript?

Possible Duplicate: Multiline strings in Javascript In Ruby you can do something like this: temp = <<-SQLCODE select * from users SQLCODE This way you have very long string literals in your code without having to escape lots of characters. Is…
Janak
  • 5,025
  • 8
  • 34
  • 43
10
votes
1 answer

bash - nested EOF

I´m trying to create a file using cat - << EOF > file.sh But inside this, I want to write another EOF. Its hard to explain, so here an example: cat - << EOF > file1.sh echo first cat - << EOF > file2.sh echo second EOF echo again first EOF But of…
Kingvinst
  • 149
  • 1
  • 7