LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-25-2007, 03:30 AM   #16
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33

Quote:
That is because bash is only so smart. When it sees a hyphen, it assumes that what follows is an option to the command (think of ls -l).
As I understand it, bash isn't looking at the command line options at all. It just passes them to ls, which is responsible for looking at them (usually via getopt(3) or getopt_long(3)). Same with rm or any other command.

Quote:
Now, long options are by definition the last thing that precedes a filename. Once bash discovers a double hyphen it stops interpreting any following single hyphens as options and it takes them literally, as parts of a filename.
I'm not sure that's true. I only have ls version 6.9 here under Cygwin, but I can say "ls --all -l" or "ls -l --all" and it produces the same effect. I believe getopt_long(3) will parse its arguments in any order, long or short.

Quote:
It doesn't matter whether your double hyphens are actually accompanied by a long option or whether they are just that, double hyphens without an option.
The double hyphen without an option name is a special case that is checked by getopt(3) and causes option parsing to stop. So everything that follows is taken by the program, ls in this case, as "stuff to work on". To quote from the getopt(3) man page, "The special argument "--" forces an end of option-scanning regardless of the scanning mode."

So here's how I think it all works...

Bash parses the command line and splits it into arguments by looking for spaces and tabs (allowing for quoting etc). It knows nothing about hyphened options. It invokes the binary by looking up the first argument in $PATH, and passes all the command line arguments (including the program name) as the argc, argv[] parameter to main(). The main (or an early) function in the program (ls or rm etc) calls getopt_long, specifying all the legal parameters, long and short. Getopt_long parses the argv[] array, accepts long and short arguments in any order until it runs out of arguments, runs out of things that start with "-" or it reaches "--". If anything remains, these are non-option arguments and the program treats them as such.

Most programs use getopt or getopt_long, which is why "--" works with most programs. Bash isn't involved, which is why it works with ksh and any other shell.

HTH,
-nick

Last edited by Nick_Battle; 10-25-2007 at 03:34 AM. Reason: typo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"bad interpreter : no such file or directory" when configure "flex" acer_peri Linux - Software 10 11-10-2010 01:19 AM
How to remove a file called: "-be" zepplin611 AIX 7 08-21-2004 03:37 PM
oops - made a file called "--dereference" - how do I delete it? BrianK Linux - General 2 06-21-2004 08:37 PM
How do you delete a file called "--tape-lengh=1"? I've tried everything... Huddlebum Linux - General 1 01-01-2004 10:11 PM
RH9 NIS login error: "home directory does not exist" airrr Linux - Networking 2 08-06-2003 01:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration