Outputting the results of a nested for loop to a file in bash
up vote
0
down vote
favorite
I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done
however I want my results to be out in the following fashion
01
02
03
etc.
But the result I am getting is similar to this
0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5
How can I solve this ?
bash shell
|
show 1 more comment
up vote
0
down vote
favorite
I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done
however I want my results to be out in the following fashion
01
02
03
etc.
But the result I am getting is similar to this
0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5
How can I solve this ?
bash shell
2
If you don't want the space between the two digits, then you can just omit it. Or - better - useprintf
for formatted outputprintf '%d%dn' "$i" "$j"
.
– steeldriver
Jun 24 '16 at 19:59
Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00
@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01
1
Are you sure? Each should be on its own line unless you usedecho -n
. What platform are you on?
– leekaiinthesky
Jun 24 '16 at 20:44
If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done
however I want my results to be out in the following fashion
01
02
03
etc.
But the result I am getting is similar to this
0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5
How can I solve this ?
bash shell
I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done
however I want my results to be out in the following fashion
01
02
03
etc.
But the result I am getting is similar to this
0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5
How can I solve this ?
bash shell
bash shell
edited Nov 25 at 22:47
Rui F Ribeiro
38.3k1477127
38.3k1477127
asked Jun 24 '16 at 19:47
JavaFreak
4339
4339
2
If you don't want the space between the two digits, then you can just omit it. Or - better - useprintf
for formatted outputprintf '%d%dn' "$i" "$j"
.
– steeldriver
Jun 24 '16 at 19:59
Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00
@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01
1
Are you sure? Each should be on its own line unless you usedecho -n
. What platform are you on?
– leekaiinthesky
Jun 24 '16 at 20:44
If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43
|
show 1 more comment
2
If you don't want the space between the two digits, then you can just omit it. Or - better - useprintf
for formatted outputprintf '%d%dn' "$i" "$j"
.
– steeldriver
Jun 24 '16 at 19:59
Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00
@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01
1
Are you sure? Each should be on its own line unless you usedecho -n
. What platform are you on?
– leekaiinthesky
Jun 24 '16 at 20:44
If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43
2
2
If you don't want the space between the two digits, then you can just omit it. Or - better - use
printf
for formatted output printf '%d%dn' "$i" "$j"
.– steeldriver
Jun 24 '16 at 19:59
If you don't want the space between the two digits, then you can just omit it. Or - better - use
printf
for formatted output printf '%d%dn' "$i" "$j"
.– steeldriver
Jun 24 '16 at 19:59
Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00
Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00
@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01
@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01
1
1
Are you sure? Each should be on its own line unless you used
echo -n
. What platform are you on?– leekaiinthesky
Jun 24 '16 at 20:44
Are you sure? Each should be on its own line unless you used
echo -n
. What platform are you on?– leekaiinthesky
Jun 24 '16 at 20:44
If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43
If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
bash
has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)
A much more shell-like way to do this (in my opinion) is:
for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done
Here is another approach:
(set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)
I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):
set {0..5}
for i; do
for j; do
[ "$i" -le "$j" ] &&
printf '%d%dn' "$i" "$j"
done
done
add a comment |
up vote
1
down vote
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "${i}${j}n" >> numbers.txt
done
done
Should do the trick. I removed the space, I prefer using {}
's around my variables and n
puts in new line.
1
Addingn
adds an empty line, sinceecho
already outputs a newline character at the end.
– Gilles
Jun 24 '16 at 22:42
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
bash
has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)
A much more shell-like way to do this (in my opinion) is:
for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done
Here is another approach:
(set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)
I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):
set {0..5}
for i; do
for j; do
[ "$i" -le "$j" ] &&
printf '%d%dn' "$i" "$j"
done
done
add a comment |
up vote
1
down vote
accepted
bash
has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)
A much more shell-like way to do this (in my opinion) is:
for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done
Here is another approach:
(set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)
I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):
set {0..5}
for i; do
for j; do
[ "$i" -le "$j" ] &&
printf '%d%dn' "$i" "$j"
done
done
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
bash
has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)
A much more shell-like way to do this (in my opinion) is:
for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done
Here is another approach:
(set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)
I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):
set {0..5}
for i; do
for j; do
[ "$i" -le "$j" ] &&
printf '%d%dn' "$i" "$j"
done
done
bash
has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)
A much more shell-like way to do this (in my opinion) is:
for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done
Here is another approach:
(set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)
I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):
set {0..5}
for i; do
for j; do
[ "$i" -le "$j" ] &&
printf '%d%dn' "$i" "$j"
done
done
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Jun 24 '16 at 23:45
Wildcard
22.5k959164
22.5k959164
add a comment |
add a comment |
up vote
1
down vote
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "${i}${j}n" >> numbers.txt
done
done
Should do the trick. I removed the space, I prefer using {}
's around my variables and n
puts in new line.
1
Addingn
adds an empty line, sinceecho
already outputs a newline character at the end.
– Gilles
Jun 24 '16 at 22:42
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
add a comment |
up vote
1
down vote
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "${i}${j}n" >> numbers.txt
done
done
Should do the trick. I removed the space, I prefer using {}
's around my variables and n
puts in new line.
1
Addingn
adds an empty line, sinceecho
already outputs a newline character at the end.
– Gilles
Jun 24 '16 at 22:42
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
add a comment |
up vote
1
down vote
up vote
1
down vote
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "${i}${j}n" >> numbers.txt
done
done
Should do the trick. I removed the space, I prefer using {}
's around my variables and n
puts in new line.
for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "${i}${j}n" >> numbers.txt
done
done
Should do the trick. I removed the space, I prefer using {}
's around my variables and n
puts in new line.
answered Jun 24 '16 at 20:34
Zachary Brady
3,386831
3,386831
1
Addingn
adds an empty line, sinceecho
already outputs a newline character at the end.
– Gilles
Jun 24 '16 at 22:42
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
add a comment |
1
Addingn
adds an empty line, sinceecho
already outputs a newline character at the end.
– Gilles
Jun 24 '16 at 22:42
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
1
1
Adding
n
adds an empty line, since echo
already outputs a newline character at the end.– Gilles
Jun 24 '16 at 22:42
Adding
n
adds an empty line, since echo
already outputs a newline character at the end.– Gilles
Jun 24 '16 at 22:42
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
Yes, which is what they want according to the desired result specified.
– Zachary Brady
Jun 24 '16 at 22:44
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f291945%2foutputting-the-results-of-a-nested-for-loop-to-a-file-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
If you don't want the space between the two digits, then you can just omit it. Or - better - use
printf
for formatted outputprintf '%d%dn' "$i" "$j"
.– steeldriver
Jun 24 '16 at 19:59
Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00
@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01
1
Are you sure? Each should be on its own line unless you used
echo -n
. What platform are you on?– leekaiinthesky
Jun 24 '16 at 20:44
If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43