windows - 为什么回显到 Windows PowerShell 中的主机文件会在字符之间创建空格?

标签 windows powershell echo

在 Windows 8.1 上的管理员模式 PowerShell 中运行以下命令会产生奇怪的输出:

PS C:\> cat C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#

上面是hosts文件的初始内容。

PS C:\> echo "127.0.0.1 example.com" >> C:\Windows\System32\drivers\etc\hosts

我使用 echo 命令添加一行并...

PS C:\> cat C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
1 2 7 . 0 . 0 . 1   e x a m p l e . c o m     

PS C:\>

注意最后一行;它在回显的每个字符之间都有空格。

知道为什么会这样吗?

最佳答案

似乎显而易见的解决方案是不使用 echo "text">> file 格式。尝试使用添加内容:

Add-Content -Path C:\windows\System32\drivers\etc\hosts. -Value "127.0.0.1 example.com"

这将为您提供所需的输出。

关于windows - 为什么回显到 Windows PowerShell 中的主机文件会在字符之间创建空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25673026/

相关文章:

php - php geonames 时区中的 echo 状态消息

c++ - 通过单击图标、在控制台中键入其名称或从批处理文件来区分程序是否运行

windows - 在 Windows 上更改 GTK+3 外观

sql-server - 用 PowerShell 替换 SSIS?

php - 从 PHP 回显 javascript

php - 如何从使用 php echo 进行显示的 html 表中在 mysql 中插入多行

c++ - Windows中动态链接库的属性(dll)

c++ - 如何写入文件然后读回以验证其内容,确保您获得的是磁盘上的内容而不是缓存

linux - 使用ssh在远程Windows机器上进行Docker登录

xml - Powershell 在 XML 中添加字符