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
62
votes
4 answers

Advantages / inconveniences of heredoc vs nowdoc in php

As a newbie, I have been advised to preferably use heredoc compared to too many nested codes (see Unexpected T_ELSE in php code). But I can't manage to understand if there is a significant difference between heredoc and nowdoc. What would be the…
Mathieu
  • 4,587
  • 11
  • 57
  • 112
58
votes
6 answers

Python here document without newlines at top and bottom

What's the best way to have a here document, without newlines at the top and bottom? For example: print ''' dog cat ''' will have newlines at the top and bottom, and to get rid of them I have to do this: print '''dog cat''' which I find to be…
Juan
  • 3,667
  • 3
  • 28
  • 32
56
votes
7 answers

Interpolate a constant (not variable) into 'heredoc'

Consider: The value of my_const is {my_const}.

MYECHO; ?> If I put a variable inside the braces, it prints out. But not the constant. How can I do it?
Michael
  • 4,786
  • 11
  • 45
  • 68
53
votes
4 answers

Heredoc strings in C#

Is there a heredoc notation for strings in C#, preferably one where I don't have to escape anything (including double quotes, which are a quirk in verbatim strings)?
Aillyn
  • 23,354
  • 24
  • 59
  • 84
52
votes
1 answer

Use a variable within heredoc in PHP

I'm a newbie to PHP/SQL and I am trying to use a variable within a heredoc as I need to output a lot of text. I've only included the first sentence as it is enough to show the problem). My problem is that within the heredoc, the variables (see…
Mathieu
  • 4,587
  • 11
  • 57
  • 112
51
votes
5 answers

Here document as an argument to bash function

Is it possible to pass a here document as a bash function argument, and in the function have the parameter preserved as a multi-lined variable? Something along the following lines: function printArgs { echo arg1="$1" echo -n arg2= cat…
Niebelung
  • 513
  • 1
  • 4
  • 4
50
votes
2 answers

Indenting heredocs with spaces

For personal development and projects I work on, we use four spaces instead of tabs. However, I need to use a heredoc, and I can't do so without breaking the indention flow. The only working way to do this I can think of would be this: usage() { …
IBPX
  • 681
  • 1
  • 6
  • 11
45
votes
20 answers

heredoc for Windows batch?

Is there a way of specifying multiline strings in batch in a way similar to heredoc in unix shells. Something similar to: cat < out.txt bla bla .. EOF The idea is to create a customized file from a template file..
Amro
  • 123,847
  • 25
  • 243
  • 454
42
votes
4 answers

Heredoc: what does the commonly used 'EOT' actually mean?

PHP's Heredoc examples always seem to use EOT (and sometimes EOD) as the seperating string, while it is actually possible to use any string here. This works: $mystring = <<
Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
41
votes
1 answer

Can I access a variable within a heredoc in Ruby?

If I have a method def some_method p = {} string = <<-MY_TERMINATOR Example text blah blah lorem ipsum something or another MY_TERMINATOR end how can I access the variable p[:name] from within the heredoc?
ben
  • 29,229
  • 42
  • 124
  • 179
39
votes
2 answers

Using && after a heredoc in bash

I have a bash script whose commands I chain together using &&, as I want the script to stop if individual steps fail. One of the steps creates a configuration file based on a heredoc: some_command && some_command && some_command && some_command…
user41871
36
votes
8 answers

Heredoc in a Makefile?

Is this possible at all and how? Update: I need this because I create a file both from dynamic and static data. Use case: I have a test directory. Each C file produces a test executable. With SRCS = $(wildcard [a-z]*.c) I can add new tests as…
nalply
  • 26,770
  • 15
  • 78
  • 101
34
votes
4 answers

Generate script in bash and save it to location requiring sudo

In bash I can create a script with a here-doc like so as per this site: http://tldp.org/LDP/abs/html/abs-guide.html#GENERATESCRIPT ( cat <<'EOF' #!/bin/bash #? [ ] / \ = + < > : ; " , * | #/ ? < > \ : * | ” #Filename="z:"${$winFn//\//\\} echo…
D W
  • 2,979
  • 4
  • 34
  • 45
34
votes
12 answers

How do you type a tab in a bash here-document?

The definition of a here-document is here: http://en.wikipedia.org/wiki/Here_document How can you type a tab in a here-document? Such as this: cat > prices.txt << EOF coffee\t$1.50 tea\t$1.50 burger\t$5.00 EOF UPDATE: Issues dealt with in this…
D W
  • 2,979
  • 4
  • 34
  • 45
29
votes
4 answers

python -c vs python -<< heredoc

I am trying to run some piece of Python code in a Bash script, so i wanted to understand what is the difference between: #!/bin/bash #your bash code python -c " #your py code " vs python - <
Kashif
  • 357
  • 1
  • 3
  • 13
1
2
3
50 51