How do I use filenames that start with a dash "-" as command arguments?
Very frequently --
is used on the command-line to signal to a program that no more available command switches are going to be used. This is particularly useful if a file contains a dash, which the program would try to interpret as an option.
Without the
--
, there is an error generated:$ pdfgrep -i posix -find.pdf -xorg.pdf pdfgrep: invalid option -- 'f' pdfgrep: invalid option -- 'd' pdfgrep: invalid option -- '.' pdfgrep: invalid option -- 'p' pdfgrep: invalid option -- 'd' pdfgrep: invalid option -- 'f'
With the
--
used we have a successful command:$ pdfgrep -i posix -- -find.pdf -xorg.pdf -find.pdf: on the command line. Currently-implemented types are emacs (this is the default), posix-awk, -find.pdf: posix-basic, posix-egrep and posix-extended. -find.pdf: posix-basic, posix-egrep and posix-extended. -find.pdf: posix-basic, posix-egrep and posix-extended.
pdfgrep
is programmed to understand--
to mean that the following command-line arguments are not options. Most programs do the same, but not all programs understand--
. For programs that don't, the solution is to prepend the filename with./
, like this:pdfgrep -i posix ./-find.pdf ./-xorg.pdf
This should work with any command, unless for some reason the command cannot accept a path, only a pure filename.
For a general introduction to the command-line, see this useful PDF.
You prepend the file name with ./
(or another relative or absolute path that works).
This way it's portable.
Example:
for zefile in ./*.tmp
do
rm -f "$zefile"
done
The use of --
to indicate the end of options is not always available. Some commands will know it, some won't. And this will change across different systems. So in the end, it's less portable.