我有一个名为的文件,fileWithOneCommand.txt
仅用以下命令命名,如下所示
ps -aux|head -n 5
然后我编写了一个名为'test5.sh'的测试shell脚本,内容如下:
file=/home/somepath/fileWithOneCommand.txt
$file;
echo see;
cat $file;
echo see2;
$(cat $file);
echo see3;
但我无法理解结果,结果如下:
$ ./test5.sh
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 168584 10308 ? Ss Feb19 0:49 /sbin/init splash
root 2 0.0 0.0 0 0 ? S Feb19 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Feb19 0:00 [pool_workqueue_release]
root 4 0.0 0.0 0 0 ? I< Feb19 0:00 [kworker/R-rcu_g]
see
ps -aux|head -n 5
see2
error: user name does not exist
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
see3
$file应该显示变量文件的内容,所以应该输出,
ps -aux|head -n 5
但为什么输出的是ps -aux|head -n 5的执行结果,而不是只显示ps -aux|head -n 5?cat $file; 返回 ps -aux|head -n 5,但是为什么
$(cat $file);
返回错误“错误:用户名不存在”?
当我在谷歌上搜索命令替换时,它说“命令的输出替换了命令本身。 Shell 通过执行命令然后用命令的标准输出替换命令替换来操作扩展。”
所以对于
$(cat $file);
括号里面,cat $file 返回 ps -aux|head -n 5 那么为什么$(cat $file);不返回 ps -aux|head -n 5 的执行结果,而是返回错误“error:用户名不存在”呢?