Cur*_*Guy 0 linux bash shell ubuntu
我正在使用Ubuntu和bash shell.
我无法理解为什么以下命令返回整行而不是PIDs:
$ ps -ef | awk "{print $2}" | head -3
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 feb18 ? 00:00:32 /sbin/init splash
root 2 0 0 feb18 ? 00:00:00 [kthreadd]
Run Code Online (Sandbox Code Playgroud)
有什么建议?
这是一个shell报价问题.如果是双引号,shell会将"$ 2"扩展为空字符串,因为它未设置.留下awk '{print }'哪些将打印整行.
使用单引号来防止扩展:
ps -ef | awk '{print $2}' | head -3
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你可以使用ps命令来获取pid,awk这不是必需的:
ps -efho pid
Run Code Online (Sandbox Code Playgroud)