12

I have this shell command:

kill `cat -- $PIDFILE`

What the double -- does here? Why not use just

kill `cat $PIDFILE`
20

The -- tells cat not to try to parse what comes after it as command line options.

As an example, think of what would happen in the two cases if the variable $PIDFILE was defined as PIDFILE="--version". On my machine, they give the following results:

$ 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
  • 3
    It is worth noting that this behavior (while very common) is defined by the receiving program (i.e. cat) and not by the shell. – dmckee Feb 21 '10 at 2:33
  • Is there any documentation or tutorial on writing your own shell script that understands that -- means the end of command line options? I've seen ones with getopts and other techniques, but nothing discussing --. – CMCDragonkai Aug 25 '15 at 8:08
  • 3
    @CMCDragonkai You need not look any further than the getopt(1) man page: "Each parameter after a '--' parameter is always interpreted as a non-option parameter". – Mikael Auno Aug 25 '15 at 10:29
1

POSIX.1-2017

POSIX also specifies it at: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02

12.2 Utility Syntax Guidelines

Guideline 10:

The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

See also: https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean-also-known-as-bare-double-dash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.