What is the << EOF equivalent in powershell
Strings can contain newlines, why don't you use that?
PS D:\> echo "blah blah
>> beh ble" | tee file.txt
blah blah
beh ble
PS D:\> cat .\file.txt
blah blah
beh ble
So if the commands are copied from another application then type "
first, paste the string and close it with another "
. But why don't just open a text editor to paste into?
In case there can be quote characters inside the string then use here-string
PS D:\> echo @"
>> here string
>> with embedded quote '"'
>> "@
here string
with embedded quote '"'
Related videos on Youtube
not2qubit
Updated on September 18, 2022Comments
-
not2qubit over 1 year
What is the powershell equivalent to the Linux command:
tee file.txt <<EOF
Where the terminal
<input>
would be something like:blah blah <\n> beh ble <\n> EOF
Let's say that I copy the 3 lines above, and then enter some (yet-unknown) powershell command. Then paste into the terminal. (Presumably hitting Ctrl-D at the end of input, after the paste operation.) Then hoping to find all this in the file, file.txt.
I suppose we have to:
- Get-terminal input for $file and the unknown command, A.
- Wait for pipe input from shell.
- Wait for Ctrl-D and interrupt (EOF) shell input.
- Pipe the input buffer to the $file, using unknown command, B.
EDIT: 2020-01-17
I realize I missed my own point (in point 3) in the above list. The point was to not have to type Ctrl-D and instead have the shell look for the "EOF" sequence, just like the Linux bash command above does. This is done all the time in bash scripts, for creating configuration files and even other bash scripts. A feature which would be very useful also in PWSH.
The closes I've been able to get to the above, is by using the Tee-Object and "here" operator (
@'...
), like this:$ @" >> Even if you have not created a profile, >> the path of the profile file is: >> $profile. >> "@ | Tee-Object -FilePath "test.txt" -Append Even if you have not created a profile, the path of the profile file is: C:\Users\XXXX\Documents\PowerShell\Microsoft.PowerShell_profile.ps1.
But I want the tee on the left and the here-is on the right, as the last part of the command. This would essentially complete the equivalent command. The here string in Linux is quite well explained in this answer.
-
slhck over 4 yearsYou are looking for “here-strings”, see e.g. powershell.org/2019/04/hear-hear-for-here-strings and others.
-
not2qubit over 4 yearsThose "here-strings" looks to me as any other variable, so I don't see how you could fill a here string using some command.
-
phuclv over 4 years@not2qubit what do you mean by
fill a here string using some command
? the herestring in bash is filled from the shell script file or keyboard and not from any commands
-
not2qubit over 4 yearsThe whole point for doing this is not having to type anything. To automatically create config files etc. I also realized after your answer, that I wasn't clear about this. I've updated OP. I think the tricky part (if even possible) is to have the input direction coming from right to left, so that the here-is is the last part of the command.