4.3 Creating variables from STDOUT
BASH offers an easy way to store the results of a command in a variable. Simple encompass a command within parenthesis and put a $ in front.
Here we introduced the handy command seq that creates a sequence of numbers. If only two arguments are given, seq interprets these are the first and last numbers of a sequence. The call seq 1 10 used above, for instance, creates the sequence 1, 2, …, 10. But seq can also be used to create more complex sequences by providing three arguments: the first number, the increment and the last number of the sequence.
By default, seqseparates the numbers by a space or new line. We can change the separator to any string use the option -s.
We will make use of seq a lot to create dummy examples, such as this one:
Here, we first created a sequence of numbers from 1 through 10 separated by a “*“, which we then passed to bc, the BASH calculator. The result was finally stored in a variable result.