我试图cd接受从另一个命令重定向到它的目录名称。这些方法都不起作用:
$ echo $HOME | cd
$ echo $HOME | xargs cd
Run Code Online (Sandbox Code Playgroud)
这确实有效:
$ cd $(echo $HOME)
Run Code Online (Sandbox Code Playgroud)
为什么第一组命令不起作用,还有其他命令也会以这种方式失败吗?
我想知道什么时候应该使用管道,什么时候不应该使用。
例如,要杀死某些处理 pdf 文件的进程,使用管道将无法执行以下操作:
ps aux | grep pdf | awk '{print $2}'|kill
Run Code Online (Sandbox Code Playgroud)
相反,我们只能通过以下方式做到这一点:
kill $(ps aux| grep pdf| awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)
或者
ps aux | grep pdf | awk '{print $2}'| xargs kill
Run Code Online (Sandbox Code Playgroud)
根据man bash(版本4.1.2):
The standard output of command is connected via a pipe to the standard input of command2.
Run Code Online (Sandbox Code Playgroud)
对于上述场景:
grep是标准输出ps。那个有效。awk是标准输出grep。那个有效。kill是标准输出awk。那行不通。以下命令的标准输入总是从前一个命令的标准输出获得输入。
killor 一起使用rm?