Can not evoke watch command with non-integer time option

command linewwatch

I want to start the w command periodically, according to man watch the smallest possible time interval is 0.1.

I tried:

watch -n1 w        (works)
watch -n1.5 w      (does not work)
watch -n0.1 w      (does not work)

When I try to start the watch command with the n-option as non-integer, I get the error message:

watch: failed to parse argument: '0.1'

Best Answer

This is a locale problem. watch uses strtod(3), which is locale-dependent, to convert the argument to -n to a double.

To fix the problem, you need to either specify the argument to -n with a different separator:

watch -n 0,1 w

Or change your locale to a setting where the period character is used for the decimal point:

export LC_NUMERIC=en_US.UTF-8
watch -n 0.1 w


A couple of references:

  1. A relevant portion of the Linux manpage for strtod:

A decimal number consists of a nonempty sequence of decimal digits possibly containing a radix character (decimal point, locale-dependent, usually '.')

  1. You can review your current settings by running locale in your terminal:

    locale
    LANG=en_US.UTF-8
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    ...
    
  2. The source code in question can be reviewed at gitlab:

    https://gitlab.com/procps-ng/procps/blob/85fff468fa263cdd2ff1c0144579527c32333695/watch.c#L625

    https://gitlab.com/procps-ng/procps/blob/85fff468fa263cdd2ff1c0144579527c32333695/lib/strutils.c#L49

(edit 2017-09-07): updated gitlab links

Related Question