Command option argument linux

Difference between terms: «option», «argument», and «parameter»?

What are the differences between these terms: «option», «argument», and «parameter»? In man pages these terms often seem to be used interchangeably.

These terms aren’t shell-specific in any way: Arguments can be passed at a program’s execution time in a literal argv array given as an argument to an execv -style syscall without any shell involved at all.

3 Answers 3

A command is split into an array of strings named arguments. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on. These arguments are sometimes called positional parameters.

$ ls -la /tmp /var/tmp arg0 = ls arg1 = -la arg2 = /tmp arg3 = /var/tmp 

An option is a documented 1 type of argument modifying the behavior of a command, e.g. -l commonly means «long», -v verbose. -lv are two options combined in a single argument. There are also long options like —verbose (see also Using getopts to process long and short command line options). As their name suggests, options are usually optional. There are however some commands with paradoxical «mandatory options».

$ ls -la /tmp /var/tmp option1= -l option2= -a 

A parameter is an argument that provides information to either the command or one of its options, e.g. in -o file , file is the parameter of the -o option. Unlike options, whose possible values are hard coded in programs, parameters are usually not, so the user is free to use whatever string suits his/her needs. Should you need to pass a parameter that looks like an option but shouldn’t be interpreted as such, you can separate it from the beginning of the command line with a double dash: — 2 .

$ ls -la /tmp /var/tmp parameter1= /tmp parameter2= /var/tmp $ ls -l -- -a option1 = -l parameter1 = -a 

A shell parameter is anything that store a value in the context of the shell. This includes positional parameters (e.g. $1 , $2 . ), variables (e.g. $foo , $bar . ) and special character ones (e.g. $@ )

Finally, there are subcommands, also known as functions / (low-level) commands, which are used with «metacommands» that embed multiple separate commands, like busybox , git , apt-get , openssl , and the likes. With them, you might have global options preceeding the subcommand, and subcommand specific options that follow the subcommand. Unlike parameters, the list of possible subcommands is hardcoded in the command itself. e.g.:

$ busybox ls -l command = busybox subcommand = ls subcommand option1 = -l $ git --git-dir=a.git --work-tree=b -C c status -s command = git command option1 = --git-dir=a.git command option2 = --work-tree=b command option3 = -C c subcommand = status subcommand option1 = -s 

Note that some commands like test , tar , dd and find have more complex argument parsing syntax than the ones described previously and can have some or all of their arguments parsed as expressions , operands , keys and similar command specific components.

Note also that optional variable assignments and redirections, despite being processed by the shell for tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal like other command line parameters are not taken into account in my reply because they have disappeared when the command is actually called and passed its arguments.

Читайте также:  Linux mint ssh включить

1 I should have written usually documented because of course, undocumented options are still options.
2 The double dash feature need to be implemented by the program though.

Источник

Linux Command Structure

For users who are used to graphical user interface (GUI) environments where double-clicking on files will (usually) launch the associated program for the file (e.g. double-clicking on a Word document will launch MS Office), one of the most difficult concept is that the Linux/UNIX command line interface (CLI) does not work this way. Specifically, typing and entering the name of a file at the command prompt in most cases will return a command not found error.

The general structure of a Linux/UNIX command line looks like:

command [-flag(s)] [-option(s) [value]] [argument(s)]

The most important thing to note is that the command line usually starts with a “command”, which is an executable file or program (any file with the executable bit set).

Options and arguments

Options are settings built into the command program (or script), that alter the default behaviour of the program. Some commands can be used without any options or arguments (e.g. ls and pwd ), but some commands usually requires some sort of argument, ( less and more ).

In the following examples, we will use two commands to illustrate the various uses and conventions in command options and arguments.

Example 1: head

The default behaviour of head is to display the first 10 lines of an input file or stream. Consider the command:

head expects an input file or stream, so BDGP6_genes.gtf is an argument expected by the program, but is not associated with any options.

**Question** What happens if you enter `head` without specifying any files?

As show in Session 2, we can change the default behaviour of displaying 10 lines by using the option -n .

Long and short options

If you look up the man page for head , you will see that actually -n is the shortened version of the option —lines . You can check this by trying the long form, which should provide identical output:

head --lines 20 BDGP6_genes.gtf 

Options usually (but not always) have a long and short version, and by convention, the long version is preceded by — , while the short version is preceded by — .

Switches/flags

In the command above -n is an option, and “ 20 ” is the argument provided for this option. -n always requires an argument, the command will report an error if you use -n without providing an integer argument.

Some options don’t require an argument, but some can be used without any arguments. As this type of option usually changes between two types of behaviour, they are often called “switches” or “flags”.

By default, head prints file names only if multiple files are provided as argument. But we can use the option -v / —verbose to force head to always print the input file name.

head -v BDGP6_genes.gtf head --verbose BDGP6_genes.gtf 

Some commands have options that can function with or without an argument, basically if no argument is provided, a default value is assumed (e.g. sed -i ).

Читайте также:  Install i2p on linux

Combining options

Using the short form of the options, we can combine multiple options together. For example, the command ls has many different switches:

ls --all -l -S # -S and -l have no long forms 

which we can combine together as:

We have seen two options for head : -n and -v . To combine them in long form, we’d type:

head --verbose --lines 20 BDGP6_genes.gtf 

which can be shortened to:

head -vn 20 BDGP6_genes.gtf 

But if you switch the order of v and n the command will fail. This is because the option which requires an argument must go last. Therefore you cannot combine multiple options which require arguments.

You can also skip the space between n and 20 :

But this works ONLY in the short form.

Command specific short-hand

head has an even shorter form for -n . Since the most common option for head is to change the number of lines to display, it can accept an even shorter form:

However, this will only work if no other options are called. This is not a standard short form, most other programs do not support it (except for maybe tail ).

Example 2: tar

tar always expects at least one option (one of -Acdtrux ), which are function letters (or main operation mode), and usually requires -f/—file as well. Since one of the function letters are always expected as the first option, you will sometimes see the — omitted. For example, the following two commands work identically:

tar czf archive.tar.gz [files_to_add] tar -czf archive.tar.gz [files_to_add] 

However, this is not the general behaviour for command options.

First option that are mandatory and are specified without — are often sub-commands. We will see a lot of them in common bioinformatics tools, such as samtools and bedtools .

General notes

  1. It is important to remember that many of these behaviours are conventions, which are not always strictly enforced. For example, shortened options are not necessarily always shortened to just a single letter (although they usually are single-letter in UNIX/Linux commands).
  2. With the exception of -h , which is almost always reserved for help ( —help ), short form letters do not usually represent the same function in different commands. For example, -c do completely different things in ls and head . However, -v is often used for either verbose ( —verbose ) or version ( —version ), and -q is often used for quiet mode.

Hosted on GitHub Pages using the Dinky theme

Источник

What’s the difference between a flag, an option, and an argument? [closed]

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

  • ls -a (I consider -a an option)
  • sudo -u username ( -u = option, username = arg)
  • chmod 664 my-dir ( 664 = option, my-dir = arg)

I can’t think of an example that might say «this is a flag» except perhaps when looking at dir listing:

-r--------. 1 david david 3344 May 19 17:48 611056.pdf 

This has the «read flag» set for the owner, but that’s all. What’s to stop me from calling that a «read option»?

I write and edit technical documentation, primarily in DocBook XML, and I’m looking for an explanation of the difference, which is consistent and accurate as possible. However, I’m already seeing a pattern forming:

  1. flags tend to be Booleans. e.g., setenforce 0
  2. options help define how a command should behave. Some may be optional.
  3. arguments tell commands what object to operate on.
Читайте также:  Server patching in linux

I could see myself combining flags and options (some options may have a dozen possible values, but Booleans only have two). Arguments appear sufficiently different to maintain them as such.

«What’s to stop me from calling …»? O.o Nothing. Just as nothing stops me from calling a spade a tree.

To make things more confusing, I’d also call an option that does not take a parameter/argument a flag, as in your ls -a example. (To me, a flag is a boolean variable.)

// , This isn’t opinion based. There is a specific, correct, and quite useful answer to this. Re-open it, now.

Not everything in the Linux/Unix world uses Posix terminology precisely, but it does define many of these terms: pubs.opengroup.org/onlinepubs/9699919799/basedefs/…

2 Answers 2

There are no consistent definitions of the terms «option», «argument», and «flag», and there is no central authority within the software development world that could enforce their usage. This happens with much terminology: after 30+ years of using the word «directory», I now have to deal with people using the word «folder» who have been confused by Microsoft’s new-speak.

There are different ways that consensus definitions for terms can come about in programming. In the case of «argument»/»option»/»flag», canonical manuals and tutorials for programming languages have helped to enforce usage, as has the terms used in common libraries.

For instance, the things you put on the command line after a command are often called «arguments» to the command, by analogy with arguments to a function call, and this is probably partly because they are called «arguments» in the C manual (hence argc and argv ). The argparse Python library also helps to enforce the term «argument». However, I have also seen them being called «parameters».

The term «option» is derived from «optional», which implies they can be left out. The getopt C library is one use of this term. But there is precedent for «options» that are not actually optional: for example, the argparse manual states that one can create a «required option» (although it also says that this is «generally considered bad form»). Options are often preceded by a single ( — ) or double ( — , long-option) dash, but there are well-known commands that do not require or enforce dash usage for options (e.g., tar , ps , and dd ). An option can itself take an argument (e.g., -w80 and —color=always ), or occasionally multiple arguments.

«Flags» are, in my experience, the same as options, but usually do not take arguments themselves and essentially represent boolean on-off switches.

On a broader note, since every programmer has the option to try and look up some standard way of doing things and naming things, but can also reinvent the wheel without much extra cost, naming is never going to be consistent. And once you have documented your code, and it is clear what new meaning you have given to these words by giving examples, those names and meanings might just stick if there are enough people who pick them up from your code.

Источник

Оцените статью
Adblock
detector