edit this article

Introducing the -- flag

This tip introduces the -- flag. This flag is supported by many utilities but isn't always documented in the man pages. However, it can be very useful, especially when dealing with malformed filenames.

Note: Most programs that make use of --option also support the -- flag since it is a standard feature of GNU getopt().

The -- flag forces everything passed in after it to be an argument to the command. This can be extremely useful when dealing with filenames that start with a - which would normally indicate an option or flag passed to the command.

For example, assume that for some reason, there is a file named -file that needs to be deleted. Normally you would use the command rm to delete a file, but this doesn't work since the file in question starts with a -. But by using the -- flag, we force rm to recognize -file as an argument and not an option.

Code Listing 1: Using the -- flag

// Attempting to use rm results in an error as rm tries to 
// process all of the letters as options (f,i,l,e). It fails 
// on 'l' since that isn't a valid option for rm. 
% ls 
% rm -file 
rm: invalid option -- l 
Try `rm --help' for more information 
% rm -- -file 
% ls 
// -file was successfully removed

Note: For more technical information, see man 3 getopt.

From http://www.gentoo.org/news/en/gwn/20031013-newsletter.xml

rate this article:
current rating: average rating: 1.4 (7 votes) (1=very good 6=terrible)
Your rating:
Very good (1) Good (2) ok (3) average (4) bad (5) terrible (6)