8

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:

  1. Get-terminal input for $file and the unknown command, A.
  2. Wait for pipe input from shell.
  3. Wait for Ctrl-D and interrupt (EOF) shell input.
  4. 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.

3
  • 2
    You are looking for “here-strings”, see e.g. powershell.org/2019/04/hear-hear-for-here-strings and others.
    – slhck
    Commented Jan 13, 2020 at 17:15
  • Those "here-strings" looks to me as any other variable, so I don't see how you could fill a here string using some command.
    – not2qubit
    Commented Jan 14, 2020 at 23:13
  • @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
    – phuclv
    Commented Jan 15, 2020 at 2:30

1 Answer 1

2

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 '"'
1
  • 1
    The 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.
    – not2qubit
    Commented Jan 17, 2020 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .