The ‘for’ line is looping over everything returned from the ‘seq’ command,
and then it is sending each argument to the ‘echo’ command, which is
printing it; by default, echo prints something and ends it with a newline.
It may help if you explained what you thought SHOULD happen. Want them
all on one line? Try using ‘echo -n’ instead of ‘echo’.
–
Good luck.
If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…
Again, please do not tell stories like doing X will return column, Bur do it on your terminal and copy/paste it here as I did. And copy/paste between CODE tags, it is the # button in the tool bar of the post editor.
Remind that the words “string” and “column” might have different meanings in this area.
In any case, you have a for loop and the echo inside. That means that there will be as many executions of the echo command as there are executions of the loop. And those echo command each output a line with a number. it is like you would have typed:
'echo 'takes a list of arguments to be printed. It does not print exactly
what it is given unless what it is given is quoted. You’re not quoting
the ‘seq’ output so it does not matter if you have one space, ten spaces,
newlines, or tabs in there; echo will just send each thing it sees that is
NOT whitespace (in your case, strings of digits) out in one big line.
‘for’ works similarly; it cares about getting arguments which are
delimited by whitespace; again, newline, one space, ten spaces… it
doesn’t matter. As a result, for operates on each string (of digits) as a
single argument, and then sends it to echo. In the for-loop case, echo is
called individually for each thing ‘for’ sends to it, and echo by default
puts a newline after things it prints, so you get a “column” as you
described it.
–
Good luck.
If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…
Don’t loop over the command substitution because it will break if unquoted, use an array or if you really insists quote the command substitution $( ) (which is the correct way of doing it), Because of word splitting. Someone already mention that in here, or use printf as a work around.
Quoting is often neglected in shell grammar but it is the most important part it is not just a decoration.
echo "$(seq -s" " 1 +2 15)"
or
printf '%d
' $(seq -s" " 1 +2 15)
That said seq is an external utility to the shell and you don’t need it if you just want to do some counting over a loop. The cfor style for loop works with bash.
I am not a seq user but bash has nothing to do with that behaviour. Reading the seq man page it states the use of the **-s **flag which means.
-s, --separator=STRING use STRING to separate numbers (default:
)
So you told seq to use a space -s " " as a separator and you expect bash to do something else .
Since by default numbers are separated by a newline you can just do.
I do not think you need to appologize, all here try to help, inclusing you. But several times the OP was asked what he realy wanted to achieve. So people could help him in a direction that may lead to sucess. No real answer thus far.
IMHO he only told us what he saw happening. And that was all correct and per definition as it should happen. But his question (well, is it a question or is it a conclusion?) was: Can’t understand the Bash behaviour.
That is of course quite possible, because it is not all that easy when one starts using bash (or most other shells). Some tried to explain why he sees things happenning as he (and we) see them happening.
It seems that he prefers to find a solution to what he wants by struggling on with the added knowledge provided above. That is imho perfect OK. Findiing things yourself is often rewarding and not easily forgotten.
But I assume, some people (me included) are curious about what he wants