# cmp

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           cmp — compare two files
    


## SYNOPSIS



           cmp **[** -l|-s**]**_file1 file2_
    


## DESCRIPTION



           The _cmp_ utility shall compare two files. The _cmp_ utility shall
           write no output if the files are the same. Under default options,
           if they differ, it shall write to standard output the byte and
           line number at which the first difference occurred. Bytes and
           lines shall be numbered beginning with 1.
    


## OPTIONS



           The _cmp_ utility shall conform to the Base Definitions volume of
           POSIX.1‐2017, _Section 12.2_ , _Utility Syntax Guidelines_.
    
           The following options shall be supported:
    
           **-l**(Lowercase ell.) Write the byte number (decimal) and the
                     differing bytes (octal) for each difference.
    
           **-s** Write nothing to standard output or standard error when
                     files differ; indicate differing files through exit
                     status only.  It is unspecified whether a diagnostic
                     message is written to standard error when an error is
                     encountered; if a message is not written, the error is
                     indicated through exit status only.
    


## OPERANDS



           The following operands shall be supported:
    
           _file1_     A pathname of the first file to be compared. If _file1_ is
                     **'-'** , the standard input shall be used.
    
           _file2_     A pathname of the second file to be compared. If _file2_
                     is **'-'** , the standard input shall be used.
    
           If both _file1_ and _file2_ refer to standard input or refer to the
           same FIFO special, block special, or character special file, the
           results are undefined.
    


## STDIN



           The standard input shall be used only if the _file1_ or _file2_
           operand refers to standard input. See the INPUT FILES section.
    


## INPUT FILES



           The input files can be any file type.
    


## ENVIRONMENT VARIABLES



           The following environment variables shall affect the execution of
           _cmp_ :
    
           _LANG_      Provide a default value for the internationalization
                     variables that are unset or null. (See the Base
                     Definitions volume of POSIX.1‐2017, _Section 8.2_ ,
                     _Internationalization Variables_ for the precedence of
                     internationalization variables used to determine the
                     values of locale categories.)
    
           _LC_ALL_    If set to a non-empty string value, override the values
                     of all the other internationalization variables.
    
           _LC_CTYPE_  Determine the locale for the interpretation of sequences
                     of bytes of text data as characters (for example,
                     single-byte as opposed to multi-byte characters in
                     arguments).
    
           _LC_MESSAGES_
                     Determine the locale that should be used to affect the
                     format and contents of diagnostic messages written to
                     standard error and informative messages written to
                     standard output.
    
           _NLSPATH_   Determine the location of message catalogs for the
                     processing of _LC_MESSAGES_.
    


## ASYNCHRONOUS EVENTS



           Default.
    


## STDOUT



           In the POSIX locale, results of the comparison shall be written to
           standard output. When no options are used, the format shall be:
    
               "%s %s differ: char %d, line %d\n", _file1_ , _file2_ ,
                   <_byte number_ >, <_line number_ >
    
           When the **-l** option is used, the format shall be:
    
               "%d %o %o\n", <_byte number_ >, <_differing byte_ >,
                   <_differing byte_ >
    
           for each byte that differs. The first <_differing byte_ > number is
           from _file1_ while the second is from _file2_.  In both cases,
           <_byte number_ > shall be relative to the beginning of the file,
           beginning with 1.
    
           No output shall be written to standard output when the **-s** option
           is used.
    


## STDERR



           The standard error shall be used only for diagnostic messages. If
           the **-l** option is used and _file1_ and _file2_ differ in length, or if
           the **-s** option is not used and _file1_ and _file2_ are identical for
           the entire length of the shorter file, in the POSIX locale the
           following diagnostic message shall be written:
    
               "cmp: EOF on %s%s\n", <_name of shorter file_ >, <_additional info_ >
    
           The <_additional info_ > field shall either be null or a string that
           starts with a <blank> and contains no <newline> characters. Some
           implementations report on the number of lines in this case.
    
           If the **-s** option is used and an error occurs, it is unspecified
           whether a diagnostic message is written to standard error.
    


## OUTPUT FILES



           None.
    


## EXTENDED DESCRIPTION



           None.
    


## EXIT STATUS



           The following exit values shall be returned:
    
            0    The files are identical.
    
            1    The files are different; this includes the case where one
                 file is identical to the first part of the other.
    
           >1    An error occurred.
    


## CONSEQUENCES OF ERRORS



           Default.
    
           _The following sections are informative._
    


## APPLICATION USAGE



           Although input files to _cmp_ can be any type, the results might not
           be what would be expected on character special device files or on
           file types not described by the System Interfaces volume of
           POSIX.1‐2017. Since this volume of POSIX.1‐2017 does not specify
           the block size used when doing input, comparisons of character
           special files need not compare all of the data in those files.
    
           For files which are not text files, line numbers simply reflect
           the presence of a <newline>, without any implication that the file
           is organized into lines.
    
           Since the behavior of **-s** differs between implementations as to
           whether error messages are written, the only way to ensure
           consistent behavior of _cmp_ when **-s** is used is to redirect standard
           error to **/dev/null**.
    
           If error messages are wanted, instead of using **-s** standard output
           should be redirected to **/dev/null** , and anything written to
           standard error should be discarded if the exit status is 1. For
           example:
    
               silent_cmp() {
                   # compare files with no output except error messages
                   message=$(cmp "$@" 2>&1 >/dev/null)
                   status=$?
                   case $status in
                   (0|1) ;;
                   (*) printf '%s\n' "$message" ;;
                   esac
                   return $status
               }
    


## EXAMPLES



           None.
    


## RATIONALE



           The global language in _Section 1.4_ , _Utility Description Defaults_
           indicates that using two mutually-exclusive options together
           produces unspecified results. Some System V implementations
           consider the option usage:
    
               cmp -l -s ...
    
           to be an error. They also treat:
    
               cmp -s -l ...
    
           as if no options were specified. Both of these behaviors are
           considered bugs, but are allowed.
    
           The word **char** in the standard output format comes from historical
           usage, even though it is actually a byte number. When _cmp_ is
           supported in other locales, implementations are encouraged to use
           the word _byte_ or its equivalent in another language. Users should
           not interpret this difference to indicate that the functionality
           of the utility changed between locales.
    
           Some implementations report on the number of lines in the
           identical-but-shorter file case. This is allowed by the inclusion
           of the <_additional info_ > fields in the output format. The
           restriction on having a leading <blank> and no <newline>
           characters is to make parsing for the filename easier. It is
           recognized that some filenames containing white-space characters
           make parsing difficult anyway, but the restriction does aid
           programs used on systems where the names are predominantly well
           behaved.
    


## FUTURE DIRECTIONS



           Future versions of this standard may require that diagnostic
           messages are written to standard error when the **-s** option is
           specified.
    


* * *