powershell - echo "string"> Windows PowerShell 中的文件将不可打印的字符附加到文件

标签 powershell cygwin dos2unix

在 Windows PowerShell 中:

echo "string" > file.txt

在 Cygwin 中:
$ cat file.txt
:::s t r i n g

$ dos2unix file.txt
dos2unix: Skipping binary file file.txt

我想要一个简单的“字符串”在文件中。我该怎么做?即,当我说 cat file.txt我只需要“字符串”作为输出。我正在从 Windows PowerShell 回显,并且无法更改。

最佳答案

试试 echo "string" | out-file -encoding ASCII file.txt得到一个简单的 ASCII 编码的 txt 文件。

生成的文件对比:

echo "string" | out-file -encoding ASCII file.txt

将生成一个包含以下内容的文件:
73 74 72 69 6E 67 0D 0A (string..)

然而
echo "string" > file.txt

将生成一个包含以下内容的文件:
FF FE 73 00 74 00 72 00 69 00 6E 00 67 00 0D 00 0A 00 (ÿþs.t.r.i.n.g.....)

(字节顺序标记 FF FE 表示文件是 UTF-16 (LE)。UTF-16 (LE) 的签名 = 2 个字节:0xFF 0xFE 后跟 2 个字节对。xx 00 xx 00 xx 00 表示正常的 0-127 ASCII字符

关于powershell - echo "string"> Windows PowerShell 中的文件将不可打印的字符附加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8163922/

相关文章:

git - 如何在 github.com 上为双因素身份验证配置 Cygwin git?

powershell - 为什么管道参数与PSDefaultParameterValues结合使用会报错?

powershell命令弹出通过USB连接的设备第一次不起作用

python - cygwincompiler 类型错误 : '>=' not supported between instances of 'NoneType' and 'str'

bash - 即使管道不接受输入,也同时输出到管道和文件

python - dos2Unix 和 python 脚本的区别

linux - 无法在 UNIX 机器上使用 dos2unix

bash - 转换行结尾

从 CMD 运行时,PowerShell .ps1 脚本文件被删除

powershell - 在远程计算机上运行 Invoke-Command 时出现 OutOfMemory 错误