shell - unix expr command shows strange results comparing integers

06
2014-04
  • user2431763

    When I write

    expr 123 \< 5  
    

    the result is correct (0) because expr evaluates two integers.
    Also

    expr 123. \< 5  
    

    gives the expected result (1) because now it compares two strings.
    But

    expr "123" \< 5  
    

    gives 0 (I expected 1, as before)
    Why?

  • Answers
  • JdeBP
    expr 123 \< 5
    

    and

    expr "123" \< 5
    

    give the same answer because what the expr command sees in its program arguments is exactly the same in both cases. Arguments 1, 2, and 3 are 123, <, and 5 in both cases.

    Use /bin/echo instead of expr to see this more clearly.

    Escaping and quoting are handled by your shell. What commands see once run is what your command line ends up as after the shell has handled all redirections, expansions, and subtitutions, and removed all quoting.


  • Related Question

    shell - How long is long for the Unix 'file' command?
  • Questioner

    When you pass a text file to the Unix file command, it might tell you something like:

    input.txt: UTF-8 Unicode English text, with very long lines
    

    Can somebody tell me the minimum number of characters a line must contain to be considered very long? The man pages don't say anything about this and I don't want to search the source code. If someone can tell me how to put this question into a Google query which doesn't return a billion results, almost all of which are irrelevant, I'd be happy, too.


  • Related Answers
  • badcat

    From ascmagic.c in the file source package on Ubuntu:

    #include "names.h"
    
    #define MAXLINELEN 300  /* longest sane line length */
    #define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
              || (x) == 0x85 || (x) == '\f')
    

    Seems like a line needs to be more than 300 characters to be considered "very long".

  • Emil Vikström

    Over 300 chacracters, according to lines 52 and 214-215 in ascmagic.c in the source code.

    The source code can be found here (link fetched from the Debian man page for file): ftp://ftp.astron.com/pub/file/

  • Dennis Williamson

    Brute force (plus this is a program, right? so it makes it programming related?):

    $ for i in {1..301}; do printf "%${i}s" "." | file - | grep very && echo $i; done
    /dev/stdin: very short file (no magic)
    1
    /dev/stdin: ASCII text, with very long lines, with no line terminators
    301
    
  • Andrew McGregor

    A line is considered long if it has more than 300 characters.