当我通过文本控制台登录 Ubuntu 时,我可以看到一个/bin/login
进程是我的 shell 的父进程。 login
运行如下:/bin/login -p --
。如下所示:
$ ps -f -C login
UID PID PPID C STIME TTY TIME CMD
root 780 1 0 Apr27 tty2 00:00:00 /bin/login -p --
我的问题是,最后一个参数的意义是什么--
?此参数未在login
手册页中记录。
答案1
其他答案已经描述了这--
意味着什么(选项处理的结束)。在这里,我将尝试回答它在基于 TTY 的进程中是如何以及为什么出现的/bin/login
。
具体来说,如果你看一下该[Service]
部分,你应该会看到以下行:/lib/systemd/system/[email protected]
ExecStart
ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM
添加的原因--
记录在man agetty
:
SECURITY NOTICE If you use the --login-program and --login-options options, be aware that a malicious user may try to enter lognames with embedded options, which then get passed to the used login program. Agetty does check for a leading "-" and makes sure the logname gets passed as one parameter (so embedded spaces will not create yet another parameter), but depend‐ ing on how the login binary parses the command line that might not be sufficient. Check that the used login program cannot be abused this way. Some programs use "--" to indicate that the rest of the commandline should not be interpreted as options. Use this feature if available by passing "--" before the username gets passed by \u.
答案2
因为我无法将其标记为非 AksUbuntu 堆栈交换站点的重复... 我厚颜无耻地复制了这个答案。
我们man bash
可以读到Shell 内置命令部分:
除非另有说明,本节中记录的每个内置命令均以 accepts
-
开头--
表示选项的结束。
:
、true
、false
和test
内置命令不接受选项,也不会进行特殊处理。--
、exit
、logout
、break
、continue
和let
内置shift
命令接受并处理以 开头的参数,而-
无需--
。 其他接受参数但未指定为接受选项的内置命令将以 开头的参数解释为-
无效选项,并需要--
来阻止这种解释。请注意,这
echo
并不--
意味着选项的结束。
还
--
更准确地说,大多数 bash 内置命令和许多其他命令都使用双破折号 ( ) 来表示命令选项的结束,此后仅接受位置参数。
使用示例:假设您要在文件中 grep 字符串-v
- 通常-v
会被视为反转匹配含义的选项(仅显示不匹配的行),但您可以像这样--
grep 字符串:-v
grep -- -v file