這個 shell 指令中的「--」(雙破折號)是什麼意思?

這個 shell 指令中的「--」(雙破折號)是什麼意思?

我有這個 shell 命令:

kill `cat -- $PIDFILE`

雙--在這裡做什麼?為什麼不只使用

kill `cat $PIDFILE`

答案1

告訴--不要cat嘗試將其後面的內容解析為命令列選項。

舉個例子,想想如果變數$PIDFILE定義為 ,在這兩種情況下會發生什麼PIDFILE="--version"。在我的機器上,他們給出以下結果:

$ cat $PIDFILE
cat (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjorn Granlund and Richard M. Stallman.

$ cat -- $PIDFILE
cat: --version: No such file or directory

答案2

POSIX.1-2017

POSIX也指定它:http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02

12.2 實用程式語法指南

準則 10:

首先--不是選項參數的參數應該被接受作為指示選項結束的分隔符號。任何後續參數都應被視為操作數,即使它們以“-”字元開頭。

也可以看看:https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean-also-known-as-bare-double-dash

相關內容