techcomp
techcomp

Reputation: 400

What does this -0 in seq shell command means

When I run seq with -0, why it treats it as 10? I have tried it with two arguments also and with three.

praveen@praveen:~$ seq -0
1
2
3
4
5
6
7
8
9
10

seq (GNU coreutils) 8.21

Upvotes: 3

Views: 95

Answers (3)

Scott Johnson
Scott Johnson

Reputation: 369

Well the answer is more in the WTF area buuut let me explain first with the man page:

SEQ(1) User Commands NAME
seq - print a sequence of numbers

SYNOPSIS

   seq [OPTION]... LAST
   seq [OPTION]... FIRST LAST
   seq [OPTION]... FIRST INCREMENT LAST

DESCRIPTION

   Print numbers from FIRST to LAST, in steps of INCREMENT.

   Mandatory arguments to long options are mandatory for short options
   too.

   -f, --format=FORMAT
          use printf style floating-point FORMAT

   -s, --separator=STRING
          use STRING to separate numbers (default: \n)

   -w, --equal-width
          equalize width by padding with leading zeroes

   --help display this help and exit

   --version
          output version information and exit

   If FIRST or INCREMENT is omitted, it defaults to 1.  That is, an
   omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.
   The sequence of numbers ends when the sum of the current number and
   INCREMENT would become greater than LAST.  FIRST, INCREMENT, and LAST
   are interpreted as floating point values.  INCREMENT is usually
   positive if FIRST is smaller than LAST, and INCREMENT is usually
   negative if FIRST is greater than LAST.  FORMAT must be suitable for
   printing one argument of type 'double'; it defaults to %.PRECf if
   FIRST, INCREMENT, and LAST are all fixed point decimal numbers with
   maximum precision PREC, and to %g otherwise.

Now lets run YOUR command...

Seq -0

1
2
3
4
5
6
7
8
9
10

... If you read the man page you will see your not passing an argument maybe as you would expect it? Your passing it -0 as in Negative 0 if you try

seg 0

you wont get anything because well...thats that. Now if you want something interesting type which should print -0 to 15

seg -0 15  
-0
-1
-2
-3
-4
-5
-6
-7
-8
-9
.0
.1
.2
.3
.4
.5
.6
.7
.8
.9
/0
/1
/2
/3
/4
/5
/6
/7
/8
/9
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15 

So to answer your question, well, your giving it input that doesn't make expect to it so its returning weird results. You cant have -0 so if you try -ANY NUMBER, say -3 you will see they return nothing, so its maybe a built in function to return 1-10.

Upvotes: 0

myaut
myaut

Reputation: 11514

Comparing coreutils 8.23 and 8.21 source code.

Options starting with - and having all-digits:

  if (argv[optind][0] == '-'
      && ((optc = argv[optind][1]) == '.' || ISDIGIT (optc)))
    {
      /* means negative number */
      break;
    }

but after that - is not taken into account:

if (seq_fast (s1, s2))

In 8.23, this is fixed:

if (*s1 != '-' && *s2 != '-' && seq_fast (s1, s2))

You can get coreutils sources at FTP: http://ftp.gnu.org/gnu/coreutils/ File is src/seq.c

Upvotes: 2

Robert Calhoun
Robert Calhoun

Reputation: 5153

Looks like you found a bug, http://bugs.gnu.org/17800. It was fixed 2014-06-18.

Upvotes: 4

Related Questions