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
10
votes
3 answers

Can't find string terminator "str" anywhere before EOF

Why I get this error? use strict; use warnings; my $str = <
user490983
  • 103
  • 1
  • 1
  • 5
10
votes
5 answers

Loop inside "heredoc" in shell scripting

I need to execute series of commands inside an interactive program/utility with parameterized values. Is there a way to loop inside heredoc ? Like below .. Not sure if eval can be of any help here. Below example doesn't seem to work as the…
Kevin
  • 183
  • 2
  • 11
10
votes
4 answers

Makefile recipe with a here-document redirection

Does anyone know how to use a here-document redirection on a recipe? test: sh <
Julio Guerra
  • 5,523
  • 9
  • 51
  • 75
10
votes
1 answer

Heredoc on docker exec

I'm basically trying to have a heredoc be executed by Flask-migrate's shell with Flask app context Below is the command i'm trying to run inside my bash script $ docker exec -it mycontainer ./manage shell <<-EOF # shell commands to be…
lozadaOmr
  • 2,565
  • 5
  • 44
  • 58
10
votes
2 answers

Cat with heredoc does not work in zsh shell

When I try cat > myfile << EOF TEST TEXT EOF I just get an empty file. The same using echo command. What's going on? I can only think about something conflicting in the .zshenv profile file, but I have no idea about what it could be... I am using…
Alex Gidan
  • 2,619
  • 17
  • 29
10
votes
2 answers

How to give arguments after Here Document?

I want to execute a python script via “Here Document” following some arguments in my bash shell script, as follows python <<'__SCRIPT__' ... __SCRIPT__ ARG1 ARG2 ... But don't know how to give these arguments. I have tried putting them following…
Pan Ruochen
  • 1,990
  • 6
  • 23
  • 34
10
votes
1 answer

PHP equivalent to python's triple-quotes - How to print bulk / lots of HTML within PHP without escaping

Possible Duplicate: php string escaping like python’s “”“ ”“”? The triple-quotes in python escapes all quotes and newlines contained within. For example, """ this is all just one string. I can even tell you "I like pi". Notice that the single…
Alex Spencer
  • 834
  • 2
  • 12
  • 23
10
votes
2 answers

Parse error: syntax error, unexpected T_SL PHP heredoc

I keep getting the following error: Parse error: syntax error, unexpected T_SL in /home/a4999406/public_html/willingLog.html on line 70 on the following code (first line is line 70): echo <<
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
10
votes
2 answers

Working with long strings (heredocs) in Java - the readable approach?

I need to work with long strings containing line breaks in Java. Those are for HTML generation, but it is not the most important here. I'm aware Java is cripple in a way it doesn't have heredocs. But there are other mechanisms I could use: 1) String…
FolksLord
  • 992
  • 2
  • 9
  • 17
9
votes
2 answers

How can I interpolate variables in Python heredocs?

In Perl languages, I can interpolate in double quoted heredocs: Perl: #!/bin/env perl use strict; use warnings; my $job = 'foo'; my $cpus = 3; my $heredoc = <<"END"; #SBATCH job $job #SBATCH cpus-per-task $cpus END print $heredoc; Raku (F.K.A.…
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
9
votes
3 answers

conditional statements inside php heredocs syntax?

i was wondering if you can have conditional statments inside a heredocs, this is my script but it deosnt parse out the $username properly? php code: function doSomething($username) { if (isset($_SESSION['u_name'])){ $reply ='
getaway
  • 8,792
  • 22
  • 64
  • 94
9
votes
2 answers

coloring in heredocs, bash

If I have previously defined color variable like this: txtred='\e[1;31m' How would I use it in heredoc: cat << EOM [colorcode here] USAGE: EOM I mean what should I write in place of [colorcode here] to render that USAGE text red?…
branquito
  • 3,864
  • 5
  • 35
  • 60
9
votes
1 answer

creating a new text file using heredoc

In my shell script, I use heredoc block to create a file on the fly. What is the python equivalent? cat > myserver.pem << "heredoc" -----BEGIN RSA PRIVATE…
shantanuo
  • 31,689
  • 78
  • 245
  • 403
8
votes
1 answer

I don't understand why string.size returns what it does

long_string = <
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
8
votes
3 answers

Special uses of this syntax in PHP? (Triple 'Angle Brackets')

Given the following code: $myString = <<