Missing file specification after redirection operator

898 Views Asked by At

I got the following error:

| cat << EOF > config.yaml 
|      ~ 
Missing file specification after redirection operator.` 

my code in power shell is following

cat << EOF > config.yaml
serve:
  proxy:
    port: 4455 # run the proxy at port 4455
...
EOF

I am running in windows server

1

There are 1 best solutions below

0
On

It looks like you're mistakenly trying to use bash syntax in PowerShell, namely a here-document.

The PowerShell equivalent, with different syntax, is a here-string.[1]

The equivalent of the bash statement in your question in PowerShell is:

# PowerShell
@"
serve:
  proxy:
    port: 4455 # run the proxy at port 4455
...
"@ > config.yaml

Note:

  • Use @' / '@ instead as the delimiters to treat the content verbatim, i.e. to suppress string interpolation, so that $-prefixed tokens aren't interpreted as variable references, for instance. This is the equivalent of quoting (any part of) the EOF delimiter in bash (e.g. cat << 'EOF' ...)

  • Nothing may follow the opening delimiter (@"/ @') on the same line, and the closing delimiter ("@/ '@) must be at the very start of the line - not even whitespace may precede it.

  • PowerShell here-strings have no equivalent to the Bash here-document feature of stripping tab-based indentation from the content if the delimiter is prefixed with - (e.g. cat << -EOF).

Windows PowerShell caveats re character encoding:

  • >, the effective alias of Out-File, defaults to "Unicode" (UTF-16LE) in Windows PowerShell (by contrast, in PowerShell (Core) (v6+) the consistent default, across all cmdlets, is BOM-less UTF-8).

  • To control the character encoding, call Out-File - or, with text input, preferably - Set-Content with the -Encoding parameter.

    • E.g., replace > config.yaml with | Set-Content -Encoding Ascii config.yaml

    • However, note that if you need UTF-8 encoding, -Encoding utf8 will invariably create a file with a BOM in Windows PowerShell (but not in PowerShell (Core), as noted); to avoid that, direct use of .NET APIs is needed - see this answer.


[1] Not to be confused with bash's here-strings, <<<"...", which are a single-line alternative to here-documents.