Questions tagged [herestring]

Multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.

39 questions
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
  • 6,628
  • 1
  • 13
  • 37
12
votes
3 answers

Here string adds line break

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
  • 3,528
  • 5
  • 33
  • 65
5
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,276
  • 4
  • 28
  • 54
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
  • 573
  • 6
  • 19
3
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
  • 83
  • 4
3
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,105
  • 2
  • 21
  • 48
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
3
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 G
  • 1,375
  • 14
  • 21
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
  • 18,605
  • 2
  • 20
  • 44
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
2 answers

One does not simply grep into ProcessBuilder

does anyone know how to use linux grep with the java ProcessBuilder? Why does this code return an empty string when it should return "sing" ? import java.io.*; import java.util.*; public class Test2 { public static void main(String[] args) throws…
burnedWood
  • 61
  • 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
  • 24,753
  • 63
  • 173
  • 296
1
vote
1 answer

why can't I create a here-string in Powershell ISE?

Trying to create a here-string > $scriptblock =@' The string is missing the terminator: '@. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString Why am I…
Roman
  • 191
  • 4
  • 15
1
vote
1 answer

Append or write to file using here-strings

How can I write to a file or append a string to a file using ed only? I have knowledge of other editors but this particular form of writing in a bash script with ed confuses me a lot: ed fileName <<< $'a textToWriteInFile\nwq' The previous line…
1
2 3