open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Terminating Options Processing with --

ITworld 2/26/2008

Sandra Henry-Stocker, ITworld.com

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.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Sun Microsystems' - FREE 60 DAY TRIAL OFFER!
Test Sun's Newest Servers BEFORE YOU BUY. Plug Them In With Access To Full Technical Support.
100% Web Based Help Desk Software
Easy to use, customizable to meet your needs, powerful and scalable. Free online demo. Try it today!
Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Used and Refurbished HP ProCurve Switches
Lifetime Warranties, Professional Testing & Shipping on all HP Equipment Purchases!
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
 Home   Open source  Operating systems  Unix
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.