Unix Tip: Terminating options processing with --

February 26, 2008, 12:23 PM —  ITworld — 

Before we get beyond the topic of working around files and directories with oddball names, there's one more trick that's useful to know about -- how to terminate options processing with "--" (excuse the grammatical pun!). Typing -- within a Unix command essentially tells the shell to stop processing the text in the command line as if it contained command line options. The command "ls -- -l", for example, would list (if it exists) a file named "-l".

To understand how this works, think back to how the shell processes what it sees on the command line. If we type "ls -l a*", for example, the shell translates a* into a list of all files that begin with the letter "a" and then makes the appropriate system calls to provide a detailed listing of those files, complete with owners, sizes and most recently changed dates and times.

If the files you want to list all start with "-a", on the other hand, the command "ls -l -- -a*" would provide the detailed list, the double hyphens telling the system not to interpret the "-" in "-a*" as the beginning of a string of options.

To remove a file starting with a hyphen, you could use a command which includes "--" as in this example:

boson> rm -- -oddfile

Similarly, you can use the same trick to work with the file in other ways. For example, to append the contents of another file to the end of the oddly named file, you could use a command like this:

cat -- otherfile >> -oddfile

You can also use this trick to create an oddly named file in the first place -- very useful if you want to try the commands in this columns to see how they work.

touch -- -oddfile

To get a more detailed view of what is happening when you use the "--" option, start a subshell using the -x option. This will show you what the shell is doing when you type commands requiring argument expansion.

boson> /bin/bash -x

Now let's see what happens when we try to list our oddly named file normally. Notice how the argument "-o*" is expanded to "-oddfile", but the shell then goes on to complain that the "e" in the file's name is not a valid command option; all the other letters in "oddfile" happen to be valid ls command options.

bash$ ls -o*
+ ls -oddfile
ls: illegal option -- e
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]

Then, try the same thing using the "--" option.

bash$ ls -- -o*
+ ls -- -oddfile
-oddfile

The file name argument is once again expanded, but the shell has no problems with it because it doesn't interpret any portion of the file's name as a command option.

Any arguments on the command line after this point are treated as file names or other types of arguments, but not as command options.

If you just happen to have files with names that both include embedded unprintable characters and begin with a hyphen, you can use a combination of the -b and -- options to work with them:

boson> ls -b -- -o*
-o\177\177file

Thanks to Jared Still for pointing out the "--" option, one that I'd somehow overlooked in my decades of working with Unix systems.

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Free books

Build your tech library with our book giveaways.

Windows PowerShell 2.0 Unleashed
By Tyson Kopczynski, Pete Handley, Marco Shaw; Published by Sams

Windows PowerShell Unleashed will not only give you deep mastery over PowerShell but also a greater understanding of the features being introduced in PowerShell 2.0–and show you how to use it to solve your challenges in your production environment. Enter now!

 

Ubuntu Server Administration
By Michael Jang; Published by McGraw-Hill Osborne Media

Realize a dynamic, stable, and secure Ubuntu Server environment with expert guidance, tips, and techniques from a Linux professional. Ubuntu Server Administration covers every facet of system management -- from users and file systems to performance tuning and troubleshooting. Enter now!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources