linux - bash "echo"包括中间创建文件的 ">"- 请解释

标签 linux bash shell io-redirection

当我写的时候:

echo 2*3>5 is a valid inequality

在我的 bash 终端中,在我的目录中创建了一个名为 5 的新文件,其中包含:

2*3 is a valid inequality

我想知道这里到底发生了什么,为什么我会得到这个输出? 我相信很明显我是 Linux 新手! 谢谢

最佳答案

在 bash 中,重定向可以发生在行中的任何地方(但你不应该这样做!--- 见 the bash-hackers tutorial )。 Bash 采用 >5作为重定向,创建输出文件 5 ,然后处理其余参数。因此,echo 2*3 is a valid inequality发生了,这给了你在输出文件中看到的输出 5 .

你可能想要的是

echo "2*3>5 is a valid inequality"

echo '2*3>5 is a valid inequality'

(带单引​​号),其中任何一个都会为您提供您在命令行上指定为打印输出的消息。不同的是,在""内, 变量(如 $foo )将被填充,但不在 '' 内.

编辑:bash man page

redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.

关于linux - bash "echo"包括中间创建文件的 ">"- 请解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622844/

相关文章:

c++ - 在 Linux 和 Mac OS X 上检测已安装的驱动器

linux - Bash 脚本 - "tar czf ..."命令忽略参数

bash - 如何通过最后一个斜线分割路径?

bash - xargs 输出缓冲 -P parallel

bash - 如何在 shell 中使用 curl 检查内容类型

shell - 如何在 golang 或一般情况下找到进程正在使用的端口?

python - 删除大文件中的空列

linux - Linux中,任何用户的组名和其他用户的组名可以相同吗?

linux - 在保留属性/所有权的同时增量备份 linux 中的文件夹

linux - 如何判断我的标准输入是来自管道输入进程的标准输出还是标准错误