Echos of number sequences.
2 2 10 ' ' -s <<< $(echo seq)
seq
prints a sequence of numbers, so 2, 2, 10
could be printed with seq
.
man bash
can be searched with /
.
Since <<<word
expands word
and supplies it as input to the command, and ' '
cannot be expanded, <<<
might expand something in the variable made by $
.
$ seq -s 2 2 10
2232425262728292102 $
I don't understand what is going on there, but I will try to figure it out by looking at a simpler form of the problem.
$ seq -s 2 2
1222 $
$ seq 1
1
$ seq 1 2
1
2
$ seq 1 2 -s
seq: invalid floating point argument: -s
$ seq -s 1 2
1121 $
$ seq -s 1 1
11 $
$ seq -s 1 1 1
11 $
$ seq -s 1 1 1 1
11 $
seq
is similar to range()
in Python:
$ seq 1 10
1
2
3
4
5
6
7
8
9
10
$ seq -s 1 10
112131415161718191101 $
Since -s
is a flag to use string to separate numbers, I assume that the string in this example is 1
and it connects the range of numbers from 1 to 10.
Testing this hypothesis validated my assumption:
$ seq -s "a" 10
1a2a3a4a5a6a7a8a9a10a
The absence of 1
suggests that a single integer input creates a number sequence with 1
as default initial value.
$ seq 3
1
2
3
What happens if we use negative values?
The default initial value is still 1
:
$ seq -3
1
0
-1
-2
-3
The original puzzle could be every second number from 2 to 10
, separated by the whitespace ' '
, and supplied as an input to echo.
This work is fun, satisfying and although seemingly trivial, I can sense that it is deeply meaningful.
$ seq -s ' ' 2 10
2 3 4 5 6 7 8 9 10 $
$ seq -s ' ' 2 10 2
2 $
After struggling to solve this seq
puzzle I now read man seq
with much more awareness.
According to the manual, seq
is structured to accommodate first, last and increment values.
Number sequences in the spotlight:
$ seq 2 2 10
2
4
6
8
10
The aha moment: the first value is the starting point, the second is the step, the third is the last. The mystery unfolds, showcasing the structure and meaning of seq
and 2 2 10
<<<
,
Why is (` ') used for blank characters in man
?
Can echo
, the writing of arguments to the standard output be connected with <<<word
, the expansion of word
and its contribution to a command on its standard input?
$ echo <<<"a"
$ echo <<<" "
$ echo <<<"\n"
$ echo <<<seq 10
10
$ echo <<<seq 1 10
1 10
$ echo <<<seq
$ echo <<<$(1)
-bash: 1: command not found
$ echo <<<$(2)
-bash: 2: command not found
$ echo <<<$(seq)
usage: seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
$ echo <<<$(seq 10)
$ echo <<<$(seq 1 10)
$ echo <<<$(seq ' ' 10)
seq: invalid floating point argument:
$ echo <<<$(seq ' ' 2 2 10)
usage: seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
$ echo <<< $(seq ' ' 2 2 10)
usage: seq [-w] [-f format] [-s string] [-t string] [first [incr]] last
$(command)
is command substitution, the command is replaced by its output value. In our puzzle, the output is the sequence of numbers returned by seq
. Since echo
requires an argument as its input, and $(command)
is a value, <<<
supplies $(command)
to echo
.
$ echo 'hello world'
hello world
$ echo <<<'hello world'
$ echo echo <<<'hello world'
echo