Bash Read: Reading comma separated list, last element is missed
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
The output of the command below is weird to me. Why does it not give me back element 5?
$ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
0
1
2
3
4
I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
. Adding a comma works, but my input data does not have a comma. Am I missing something?
bash shell-script read shell-builtin
add a comment |
The output of the command below is weird to me. Why does it not give me back element 5?
$ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
0
1
2
3
4
I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
. Adding a comma works, but my input data does not have a comma. Am I missing something?
bash shell-script read shell-builtin
add a comment |
The output of the command below is weird to me. Why does it not give me back element 5?
$ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
0
1
2
3
4
I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
. Adding a comma works, but my input data does not have a comma. Am I missing something?
bash shell-script read shell-builtin
The output of the command below is weird to me. Why does it not give me back element 5?
$ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
0
1
2
3
4
I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
. Adding a comma works, but my input data does not have a comma. Am I missing something?
bash shell-script read shell-builtin
bash shell-script read shell-builtin
edited May 17 at 12:18
terdon♦
137k33277458
137k33277458
asked May 17 at 10:58
KarloKarlo
9882820
9882820
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
With read
, -d
is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read
returns false on EOF and the loop exits (even though the final value was read).
echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }
(Even with -d
, read
also uses $IFS
, absorbing whitespace including the trailing n
on the final value that would appear using other methods such as readarray
)
The Bash FAQ discusses this, and how to handle various similar cases:
- Bash Pitfalls #47 IFS=, read [...]
- BashFAQ 001 How can I read a file [...] line-by-line
- BashFAQ 005 How can I use array variables?
8
I guess one could doread -d, i || [[ -n $i ]]
a la What doeswhile read -r line || [[ -n $line ]]
mean?
– steeldriver
May 17 at 11:18
add a comment |
As other answers state, -d
is an end-of-line character, not a field separator. You can do
IFS=, read -a fields <<< "1,2,3,4,5"
for i in "${fields[@]}"; do echo "$i"; done
add a comment |
From man:
-d delim
The first character of delim is used to terminate the input line, rather than newline.
Your element 5 doesn't have a delimiter (comma), so it won't be read.
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
3
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
add a comment |
What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?
As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows
while read -d, i || [[ -n $i ]]; do ...
Ex.
$ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
0
1
2
3
4
5
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f519471%2fbash-read-reading-comma-separated-list-last-element-is-missed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
With read
, -d
is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read
returns false on EOF and the loop exits (even though the final value was read).
echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }
(Even with -d
, read
also uses $IFS
, absorbing whitespace including the trailing n
on the final value that would appear using other methods such as readarray
)
The Bash FAQ discusses this, and how to handle various similar cases:
- Bash Pitfalls #47 IFS=, read [...]
- BashFAQ 001 How can I read a file [...] line-by-line
- BashFAQ 005 How can I use array variables?
8
I guess one could doread -d, i || [[ -n $i ]]
a la What doeswhile read -r line || [[ -n $line ]]
mean?
– steeldriver
May 17 at 11:18
add a comment |
With read
, -d
is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read
returns false on EOF and the loop exits (even though the final value was read).
echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }
(Even with -d
, read
also uses $IFS
, absorbing whitespace including the trailing n
on the final value that would appear using other methods such as readarray
)
The Bash FAQ discusses this, and how to handle various similar cases:
- Bash Pitfalls #47 IFS=, read [...]
- BashFAQ 001 How can I read a file [...] line-by-line
- BashFAQ 005 How can I use array variables?
8
I guess one could doread -d, i || [[ -n $i ]]
a la What doeswhile read -r line || [[ -n $line ]]
mean?
– steeldriver
May 17 at 11:18
add a comment |
With read
, -d
is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read
returns false on EOF and the loop exits (even though the final value was read).
echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }
(Even with -d
, read
also uses $IFS
, absorbing whitespace including the trailing n
on the final value that would appear using other methods such as readarray
)
The Bash FAQ discusses this, and how to handle various similar cases:
- Bash Pitfalls #47 IFS=, read [...]
- BashFAQ 001 How can I read a file [...] line-by-line
- BashFAQ 005 How can I use array variables?
With read
, -d
is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read
returns false on EOF and the loop exits (even though the final value was read).
echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }
(Even with -d
, read
also uses $IFS
, absorbing whitespace including the trailing n
on the final value that would appear using other methods such as readarray
)
The Bash FAQ discusses this, and how to handle various similar cases:
- Bash Pitfalls #47 IFS=, read [...]
- BashFAQ 001 How can I read a file [...] line-by-line
- BashFAQ 005 How can I use array variables?
edited May 17 at 17:15
answered May 17 at 11:09
mr.spuraticmr.spuratic
7,3611229
7,3611229
8
I guess one could doread -d, i || [[ -n $i ]]
a la What doeswhile read -r line || [[ -n $line ]]
mean?
– steeldriver
May 17 at 11:18
add a comment |
8
I guess one could doread -d, i || [[ -n $i ]]
a la What doeswhile read -r line || [[ -n $line ]]
mean?
– steeldriver
May 17 at 11:18
8
8
I guess one could do
read -d, i || [[ -n $i ]]
a la What does while read -r line || [[ -n $line ]]
mean?– steeldriver
May 17 at 11:18
I guess one could do
read -d, i || [[ -n $i ]]
a la What does while read -r line || [[ -n $line ]]
mean?– steeldriver
May 17 at 11:18
add a comment |
As other answers state, -d
is an end-of-line character, not a field separator. You can do
IFS=, read -a fields <<< "1,2,3,4,5"
for i in "${fields[@]}"; do echo "$i"; done
add a comment |
As other answers state, -d
is an end-of-line character, not a field separator. You can do
IFS=, read -a fields <<< "1,2,3,4,5"
for i in "${fields[@]}"; do echo "$i"; done
add a comment |
As other answers state, -d
is an end-of-line character, not a field separator. You can do
IFS=, read -a fields <<< "1,2,3,4,5"
for i in "${fields[@]}"; do echo "$i"; done
As other answers state, -d
is an end-of-line character, not a field separator. You can do
IFS=, read -a fields <<< "1,2,3,4,5"
for i in "${fields[@]}"; do echo "$i"; done
edited May 17 at 13:12
Scott
7,24852953
7,24852953
answered May 17 at 12:51
Graham BreedGraham Breed
811
811
add a comment |
add a comment |
From man:
-d delim
The first character of delim is used to terminate the input line, rather than newline.
Your element 5 doesn't have a delimiter (comma), so it won't be read.
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
3
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
add a comment |
From man:
-d delim
The first character of delim is used to terminate the input line, rather than newline.
Your element 5 doesn't have a delimiter (comma), so it won't be read.
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
3
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
add a comment |
From man:
-d delim
The first character of delim is used to terminate the input line, rather than newline.
Your element 5 doesn't have a delimiter (comma), so it won't be read.
From man:
-d delim
The first character of delim is used to terminate the input line, rather than newline.
Your element 5 doesn't have a delimiter (comma), so it won't be read.
edited May 17 at 12:18
terdon♦
137k33277458
137k33277458
answered May 17 at 11:09
msp9011msp9011
5,09044269
5,09044269
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
3
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
add a comment |
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
3
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
So the best fix is putting another comma after the input?
– Karlo
May 17 at 11:09
3
3
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.
– Jeff Schaller♦
May 17 at 11:15
add a comment |
What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?
As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows
while read -d, i || [[ -n $i ]]; do ...
Ex.
$ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
0
1
2
3
4
5
add a comment |
What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?
As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows
while read -d, i || [[ -n $i ]]; do ...
Ex.
$ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
0
1
2
3
4
5
add a comment |
What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?
As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows
while read -d, i || [[ -n $i ]]; do ...
Ex.
$ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
0
1
2
3
4
5
What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?
As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows
while read -d, i || [[ -n $i ]]; do ...
Ex.
$ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
0
1
2
3
4
5
answered May 17 at 13:24
steeldriversteeldriver
39.9k45493
39.9k45493
add a comment |
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.
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%2f519471%2fbash-read-reading-comma-separated-list-last-element-is-missed%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