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
3397
votes
43 answers

Creating multiline strings in JavaScript

I have the following code in Ruby. I want to convert this code into JavaScript. What is the equivalent code in JS? text = <<"HERE" This Is A Multiline String HERE
Newy
  • 38,977
  • 9
  • 43
  • 59
984
votes
12 answers

How does "cat << EOF" work in bash?

I needed to write a script to enter multi-line input to a program (psql). After a bit of googling, I found the following syntax works: cat << EOF | psql ---params BEGIN; `pg_dump ----something` update table .... statement ...; END; EOF This…
hasen
  • 161,647
  • 65
  • 194
  • 231
966
votes
11 answers

How can I write a heredoc to a file in Bash script?

How can I write a here document to a file in Bash script?
Joshua Enfield
  • 17,642
  • 10
  • 51
  • 98
526
votes
13 answers

How to assign a heredoc value to a variable in Bash?

I have this multi-line string (quotes included): abc'asdf" $(dont-execute-this) foo"bar"'' How would I assign it to a variable using a heredoc in Bash? I need to preserve newlines. I don't want to escape the characters in the string, that would be…
Neil
  • 24,551
  • 15
  • 60
  • 81
263
votes
3 answers

Using variables inside a bash heredoc

I'm trying to interpolate variables inside of a bash heredoc: var=$1 sudo tee "/path/to/outfile" > /dev/null << "EOF" Some text that contains my $var EOF This isn't working as I'd expect ($var is treated literally, not expanded). I need to use sudo…
Jon
  • 3,280
  • 2
  • 15
  • 16
263
votes
18 answers

Executing multi-line statements in the one-line command-line

I'm using Python with -c to execute a one-liner loop, i.e.: python -c "for r in range(10): print 'rob'" This works fine. However, if I import a module before the for loop, I get a syntax error: python -c "import sys; for r in range(10): print…
user248237
208
votes
5 answers

What is the advantage of using heredoc in PHP?

What is the advantage of using heredoc in PHP, and can you show an example?
johnlemon
  • 20,761
  • 42
  • 119
  • 178
191
votes
7 answers

How to cat <> a file containing code?

I want to print code into a file using cat <>: cat <> brightup.sh !/bin/bash curr=`cat /sys/class/backlight/intel_backlight/actual_brightness` if [ $curr -lt 4477 ]; then curr=$((curr+406)); echo $curr >…
UberNate
  • 2,109
  • 2
  • 15
  • 15
156
votes
4 answers

Multiline syntax for piping a heredoc; is this portable?

I'm familiar with this syntax: cmd1 << EOF | cmd2 text EOF but just discovered that bash allows me to write: cmd1 << EOF | text EOF cmd2 (the heredoc is used as input to cmd1, and the output of cmd1 is piped to cmd2). This seems like a very odd…
William Pursell
  • 204,365
  • 48
  • 270
  • 300
137
votes
2 answers

How to avoid heredoc expanding variables?

I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping export PLACEHOLDER1="myPlaceholder1Value" sudo /bin/su -c "cat << EOF >…
TheCodeKiller
  • 1,733
  • 2
  • 12
  • 18
132
votes
14 answers

Javascript heredoc

I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality. I found this: heredoc = '\
\ \
'; I think it will work for me.…
VeroLom
  • 3,856
  • 9
  • 34
  • 48
119
votes
9 answers

here-document gives 'unexpected end of file' error

I need my script to send an email from terminal. Based on what I've seen here and many other places online, I formatted it like this: /var/mail -s "$SUBJECT" "$EMAIL" << EOF Here's a line of my message! And here's another line! Last line of the…
thnkwthprtls
  • 3,287
  • 11
  • 42
  • 63
104
votes
17 answers

Calling PHP functions within HEREDOC strings

In PHP, the HEREDOC string declarations are really useful for outputting a block of html. You can have it parse in variables just by prefixing them with $, but for more complicated syntax (like $var[2][3]), you have to put your expression inside {}…
Doug Kavendek
  • 3,624
  • 4
  • 31
  • 43
103
votes
11 answers

How do I remove leading whitespace chars from Ruby HEREDOC?

I'm having a problem with a Ruby heredoc i'm trying to make. It's returning the leading whitespace from each line even though i'm including the - operator, which is supposed to suppress all leading whitespace characters. my method looks like this: …
Chris Drappier
  • 5,280
  • 10
  • 40
  • 64
76
votes
8 answers

In PHP, what does "<<<" represent?

For example: $sql = <<
Chrish
  • 977
  • 1
  • 8
  • 14
1
2 3
50 51