Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

New to Linux and have a few questions about command line arguments:

Let's say there a command-line tool called widget that takes an argument called fizz. I've seen all different types of arguments:

widget -fizz
widget --fizz
widget \fizz
widget -f

Whats the difference in how these are used? Does the command-line tool decide how to determine them differently, or does Linux know they all mean the same thing, but feeds them to the application differently?

Could I customize my own way of passing the (already existing) widget tool its fizz argument:

widget #f#

Thanks in advance!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Whats the difference in how these are used?

That entirely depends on the program, because...

Does the command-line tool decide how to determine them differently, or does Linux know they all mean the same thing, but feeds them to the application differently?

...the arguments to the process come in as an array of individual strings.

There are two things involved here: one is the shell, which parses the input you type and executes the process. That, for example, splits the string on whitespace and passes those to widget. See, for example, man sh and man bash for details on how they handle those things.

Ultimately, though, widget gets the argument -fizz or --fizz exactly as it is written, as a string. It decides what to do with it.

There are some standards - lots of variants of getopts and popt that process those arguments for widget, and they have some degree of "standard" behaviour.

Nothing stops the author of widget inventing their own standard though.

So, finally, if you are the author of widget or otherwise modify the source code you can absolutely make it treat #f# as the fizz argument. You can't do it without changing the program though.

share|improve this answer
1  
"widget gets the argument -fizz or --fizz exactly as it is written" Well, after alias substitution, variable substitution, glob expansion and history substitution. –  dmckee Feb 5 '12 at 0:30

Does the command-line tool decide how to determine them differently, or does Linux know they all mean the same thing, but feeds them to the application differently?

It's the job of the exectuable, or function, to evaluate its arguments. The shell (not Linux, but bash, csh, etc.) will not interfere with these arguments at all.

This means that every argument is treated differently in your case. Note however that the shell performs expansions or substitutions before the command gets its arguments. When you, for example, have a folder with two JPG images in it, and write:

ls *.jpg

Then the shell will expand *.jpg to image1.jpg image2.jpg, therefore resulting in ls being passed the following arguments:

ls image1.jpg image2.jpg

Other than that, you can't really modify a script to take another argument than what is already compiled or written into the executable, other than rewriting the source code.

Also check out the following question:

What's the difference between one-dash and two-dashes for command prompt parameters?

share|improve this answer

First of all, you're unlikely to see any argument beginning with \ in Linux because in most shells this is an escape character, used for escaping spaces and other characters which are meaningful to the shell ($, "", (), etc.).

The Linux shell (Bash in most distros) merely passes each space-separated argument as strings to the process. What the process decides to do with them is the programmer's choice.

Usually the - prefix indicates a short option (such as -h), and the -- indicates a longer version (such as --help). This is part of the POSIX standard that most Linux programs use, which was inherited from early Unix tools. See the getopt article on Wikipedia for more information.

For your last question, no, you can't modify the way the widget tool interprets these arguments, unless you modify its source code.

On some Unix systems there's a nifty utility called pargs that lets you see all the arguments that were passed to the process. In Linux, you can replicate this using

ps eww -p 12345

Where 12345 is the process identifier (PID). See this answer for an alternative.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.