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.
$ ls -l | sort -k 5 -n -r
$ ls -l | sort -k5nr

I find out these two command generate the same output.
But I don't understand why I can combine 5 with n?
Why not a syntax error?

edit:

$ ls -l | cut -d: -n -f 2    
$ ls -l | cut -d:nf2
cut: the delimiter must be a single character
Try `cut --help' for more information.

Why cut dose not behave like sort?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Because sort is implemented in a way that parses these in the way you expect.

See also here:

  • Sometimes options and their arguments are run together, sometimes separated by whitespace, and sometimes by a character, typically : or =. Thus "Prog -fFilename", "Prog -f Filename", "Prog -f:Filename", "Prog -f=Filename".
  • Some programs allow single-character options to be combined; others do not. The switch "-fA" may mean the same as "-f -A", or it may be incorrect, or it may even be a valid but different parameter.

This looks like a combination of both (works without whitespace, and combination of single-character options).


They are simply different programs whose argument parsing is implemented differently.

In coreutils 8.13, compare the following::

  • src/sort.c line 4315, invoking the special integer parsing function parse_field_count, that returns with the first invalid character (i.e. once the number value is finished and the next option starts): That's why sort can handle your arguments.
  • src/cut.c, line 803 ff., simply using the regular getopt behavior of interpreting everything until the next white space as parameter to the current option.
share|improve this answer
    
does sort follow the general rule? cut doesn't work that way. –  kev Dec 24 '11 at 8:16
    
@kev Added additional explanation to my answer. –  Daniel Beck Dec 24 '11 at 19:38
    
+1 Thank you very much. –  kev Dec 25 '11 at 10:00
add comment

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.