Multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.
Questions tagged [herestring]
50 questions
70
votes
4 answers
What does the Bash operator <<< (i.e. triple less than sign) mean?
What does the triple-less-than-sign bash operator, <<<, mean, as inside the following code block?
LINE="7.6.5.4"
IFS=. read -a ARRAY <<< "$LINE"
echo "$IFS"
echo "${ARRAY[@]}"
Also, why does $IFS remain to be a space, not a period?

gsklee
- 4,774
- 4
- 39
- 55
22
votes
3 answers
How can I stop a here string (<<<) from adding a line break or new lines?
It seems that here string is adding line break. Is there a convenient way of removing it?
$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6 -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249 -
$ md5sum…

NarūnasK
- 4,564
- 8
- 50
- 76
16
votes
1 answer
Do here-strings undergo word-splitting?
Quoting Bash Reference Manual and man bash (version 4.3):
[n]<<< word
The word undergoes brace expansion, tilde expansion, parameter and
variable expansion, command substitution, arithmetic expansion, and
quote removal. Pathname expansion and…

PesaThe
- 7,259
- 1
- 19
- 43
7
votes
3 answers
prepend **heredoc** to a file, in bash
I was using:
cat <<<"${MSG}" > outfile
to begin with writing a message to outfile, then further processing goes on,
which appends to that outfile from my awk script.
But now logic has changed in my program, so I'll have to first populate
outfile by…

branquito
- 3,864
- 5
- 35
- 60
5
votes
2 answers
While-loop over lines from variable in bash
Assume a file file with multiple lines.
$ cat file
foo
bar
baz
Assume further that I wish to loop through each line with a while-loop.
$ while IFS= read -r line; do
$ echo $line
$ # do stuff
$ done < file
foo
bar
baz
Finally, please assume…

Michael Gruenstaeudl
- 1,609
- 1
- 17
- 31
4
votes
2 answers
How to set encoding for a herestring/heredoc in powershell?
I'm attempting to update the hosts file on a Windows server and trying to do it using a heredoc in powershell.
I can't figure out why my result has extra spaces between every character in each hosts entry.
I'm porting some scripting from Linux.
PS…

mikedoy
- 117
- 1
- 6
4
votes
1 answer
Where does PowerShell get the newline characters it puts in here strings?
Consider the following PowerShell code:
@"
"@.GetEnumerator() | %{[int]$_}
On my computer this outputs
13
10
which are the decimal representation of the ASCII control characters for carriage return and line feed, respectively.
The same code…

alx9r
- 3,675
- 4
- 26
- 55
4
votes
3 answers
PowerShell Nested Here-String
Placing the following within a script will fail:
$MyString = @'
hello
@'
'@
bye
'@
write-host $MyString
The error returned is as follows:
At C:\scripts\test.ps1:6 char:1
+ '@
+ ~~
The string is missing the terminator: '.
+ CategoryInfo …

Fitzroy
- 229
- 2
- 7
4
votes
3 answers
Is it possible to reevalute here strings after I declare them?
I'm trying to use PowerShell for a small code generation task. I want the script to generate some java classes and interfaces.
In the script I want to declare a here string variable. For example:
$controllerContent = @"
package controller;
import…

Patrick
- 585
- 8
- 22
3
votes
2 answers
Running a command that includes an STDIN redirect with screen
Using the Gradle SSH plugin I deploy a .jar file to another machine.
I then would like to run that jar in a detached screen session.
This wouldn't be a problem if the application did not require input on STDIN, running screen -dmS screen-name java…

Markus Weninger
- 11,931
- 7
- 64
- 137
3
votes
2 answers
Bash bc returns (standard_in) 1: parse error only when part of for loop
I'm iterating through an array of integers ${startTimes} (marker locations in an audio file, in samples) and using bc to convert those integers to milliseconds. I'm passing the results into a new array ${msValuesArray}. If I run each array element…

Urphänomen
- 81
- 1
- 8
2
votes
1 answer
Why does the read command in bash work with a herestring but not with piped output?
I'm trying to understand how the bash read command works under the hood.
Given that it expects its input to come from standard input, I was surprised to find out that piped input does not work as expected. E.g.
### Pipe scenario
echo "1 2 3" | read…

Tasos Papastylianou
- 21,371
- 2
- 28
- 57
2
votes
2 answers
How to use a $_. variable in a herestring?
I can't seem to figure out how to use a variable in a herestring, and for the variable to be expanded at a later time in a piped command. I have experimented with single ' and double " quotes, and escape ` characters.
I am trying to use the…

J. Bond
- 23
- 3
2
votes
2 answers
Find and replace a string containing both double quotes and brackets
Let's say I have a test file named testfile.txt containing the below line:
one (two) "three"
I want to use PowerShell to say that if the entire string exists, place a line directly underneath it with the value:
four (five) "six"
(Notice that it…

coursemyhorse
- 23
- 2
- 6
2
votes
5 answers
sh - split string by delimiter
code
s='id;some text here with possible ; inside'
IFS=';' read -r id string <<< "$s"
echo "$id"
error
restore.sh: 2: restore.sh: Syntax error: redirection unexpected
bash version GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

clarkk
- 27,151
- 72
- 200
- 340