Iterate through multiline string line by line The Next CEO of Stack OverflowWhy is printf better than echo?Why is using a shell loop to process text considered bad practice?Are there naming conventions for variables in shell scripts?Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?Understanding IFSWhat should interactive shells do in orphaned process groups?for loop to iterate through some file nth positionCron only occasionally sends e-mail on output and errorsIterate Through Sets of Command Arguments in Bashhow to iterate through files in directory excluding hidden filesStarting an interactive shell as an asynchronous process (signal delivery)Control characters in a terminal with an active foreground processhow to let sudo fork bash instead of sh?Multiline command : comment out one line

Expectation in a stochastic differential equation

What is the difference between Statistical Mechanics and Quantum Mechanics

IC has pull-down resistors on SMBus lines?

How many extra stops do monopods offer for tele photographs?

Is it okay to majorly distort historical facts while writing a fiction story?

Can I board the first leg of the flight without having final country's visa?

Is dried pee considered dirt?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

What CSS properties can the br tag have?

Why the last AS PATH item always is `I` or `?`?

Is a distribution that is normal, but highly skewed, considered Gaussian?

Yu-Gi-Oh cards in Python 3

How to set page number in right side in chapter title page?

Ubuntu - "sudo iptables" command works in terminal, doesn't work in bash script

Prepend last line of stdin to entire stdin

Getting Stale Gas Out of a Gas Tank w/out Dropping the Tank

Is there a reasonable and studied concept of reduction between regular languages?

How to get the last not-null value in an ordered column of a huge table?

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

"Eavesdropping" vs "Listen in on"

Lucky Feat: How can "more than one creature spend a luck point to influence the outcome of a roll"?

How did Beeri the Hittite come up with naming his daughter Yehudit?

Are the names of these months realistic?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis



Iterate through multiline string line by line



The Next CEO of Stack OverflowWhy is printf better than echo?Why is using a shell loop to process text considered bad practice?Are there naming conventions for variables in shell scripts?Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?Understanding IFSWhat should interactive shells do in orphaned process groups?for loop to iterate through some file nth positionCron only occasionally sends e-mail on output and errorsIterate Through Sets of Command Arguments in Bashhow to iterate through files in directory excluding hidden filesStarting an interactive shell as an asynchronous process (signal delivery)Control characters in a terminal with an active foreground processhow to let sudo fork bash instead of sh?Multiline command : comment out one line










1















I would like to process a multiline string and iterate it line by line, in a POSIX shell (/bin/sh) on a BSD platform. Bash is not included in the base BSD-distribution and has a GPL license - so I am trying to make it universally work with /bin/sh instead.



I found a solution using a pipe, however in the regular /bin/sh shell, these a processed in a separate process, meaning the following does not work:



MULTILINE="`cat $SOMEFILE`"
SOMEVAR="original value"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
echo "this is a single line: $SINGLELINE"
echo "SOMEVAR is now: $SOMEVAR"
done

echo "Final SOMEVAR is unchanged: $SOMEVAR"


In the above example, it accomplishes what I want, except for the fact that changes to variables such as $SOMEVAR are not accessible outside the while loop.



My question: how can I accomplish something like the above without this restriction? Note that many solutions require Bash, whereas I am using the standard POSIX-shell /bin/sh.










share|improve this question
























  • Why can you not use bash? (add to your question)

    – ctrl-alt-delor
    9 hours ago











  • I can not reproduce your issue using /bin/sh on a BSD system. There is nothing in this code that would require bash.

    – Kusalananda
    9 hours ago












  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now. The example indeed does not require Bash.

    – Steiner
    9 hours ago











  • @Steiner In fact, it would have the same issue in bash.

    – Kusalananda
    9 hours ago






  • 3





    There is no Bourne shell on the BSDs. Nor is the Bourne shell standard. Do not conflate the Bourne shell with a POSIX-conformant sh.

    – JdeBP
    9 hours ago















1















I would like to process a multiline string and iterate it line by line, in a POSIX shell (/bin/sh) on a BSD platform. Bash is not included in the base BSD-distribution and has a GPL license - so I am trying to make it universally work with /bin/sh instead.



I found a solution using a pipe, however in the regular /bin/sh shell, these a processed in a separate process, meaning the following does not work:



MULTILINE="`cat $SOMEFILE`"
SOMEVAR="original value"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
echo "this is a single line: $SINGLELINE"
echo "SOMEVAR is now: $SOMEVAR"
done

echo "Final SOMEVAR is unchanged: $SOMEVAR"


In the above example, it accomplishes what I want, except for the fact that changes to variables such as $SOMEVAR are not accessible outside the while loop.



My question: how can I accomplish something like the above without this restriction? Note that many solutions require Bash, whereas I am using the standard POSIX-shell /bin/sh.










share|improve this question
























  • Why can you not use bash? (add to your question)

    – ctrl-alt-delor
    9 hours ago











  • I can not reproduce your issue using /bin/sh on a BSD system. There is nothing in this code that would require bash.

    – Kusalananda
    9 hours ago












  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now. The example indeed does not require Bash.

    – Steiner
    9 hours ago











  • @Steiner In fact, it would have the same issue in bash.

    – Kusalananda
    9 hours ago






  • 3





    There is no Bourne shell on the BSDs. Nor is the Bourne shell standard. Do not conflate the Bourne shell with a POSIX-conformant sh.

    – JdeBP
    9 hours ago













1












1








1








I would like to process a multiline string and iterate it line by line, in a POSIX shell (/bin/sh) on a BSD platform. Bash is not included in the base BSD-distribution and has a GPL license - so I am trying to make it universally work with /bin/sh instead.



I found a solution using a pipe, however in the regular /bin/sh shell, these a processed in a separate process, meaning the following does not work:



MULTILINE="`cat $SOMEFILE`"
SOMEVAR="original value"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
echo "this is a single line: $SINGLELINE"
echo "SOMEVAR is now: $SOMEVAR"
done

echo "Final SOMEVAR is unchanged: $SOMEVAR"


In the above example, it accomplishes what I want, except for the fact that changes to variables such as $SOMEVAR are not accessible outside the while loop.



My question: how can I accomplish something like the above without this restriction? Note that many solutions require Bash, whereas I am using the standard POSIX-shell /bin/sh.










share|improve this question
















I would like to process a multiline string and iterate it line by line, in a POSIX shell (/bin/sh) on a BSD platform. Bash is not included in the base BSD-distribution and has a GPL license - so I am trying to make it universally work with /bin/sh instead.



I found a solution using a pipe, however in the regular /bin/sh shell, these a processed in a separate process, meaning the following does not work:



MULTILINE="`cat $SOMEFILE`"
SOMEVAR="original value"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
echo "this is a single line: $SINGLELINE"
echo "SOMEVAR is now: $SOMEVAR"
done

echo "Final SOMEVAR is unchanged: $SOMEVAR"


In the above example, it accomplishes what I want, except for the fact that changes to variables such as $SOMEVAR are not accessible outside the while loop.



My question: how can I accomplish something like the above without this restriction? Note that many solutions require Bash, whereas I am using the standard POSIX-shell /bin/sh.







shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 hours ago







Steiner

















asked 9 hours ago









SteinerSteiner

858




858












  • Why can you not use bash? (add to your question)

    – ctrl-alt-delor
    9 hours ago











  • I can not reproduce your issue using /bin/sh on a BSD system. There is nothing in this code that would require bash.

    – Kusalananda
    9 hours ago












  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now. The example indeed does not require Bash.

    – Steiner
    9 hours ago











  • @Steiner In fact, it would have the same issue in bash.

    – Kusalananda
    9 hours ago






  • 3





    There is no Bourne shell on the BSDs. Nor is the Bourne shell standard. Do not conflate the Bourne shell with a POSIX-conformant sh.

    – JdeBP
    9 hours ago

















  • Why can you not use bash? (add to your question)

    – ctrl-alt-delor
    9 hours ago











  • I can not reproduce your issue using /bin/sh on a BSD system. There is nothing in this code that would require bash.

    – Kusalananda
    9 hours ago












  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now. The example indeed does not require Bash.

    – Steiner
    9 hours ago











  • @Steiner In fact, it would have the same issue in bash.

    – Kusalananda
    9 hours ago






  • 3





    There is no Bourne shell on the BSDs. Nor is the Bourne shell standard. Do not conflate the Bourne shell with a POSIX-conformant sh.

    – JdeBP
    9 hours ago
















Why can you not use bash? (add to your question)

– ctrl-alt-delor
9 hours ago





Why can you not use bash? (add to your question)

– ctrl-alt-delor
9 hours ago













I can not reproduce your issue using /bin/sh on a BSD system. There is nothing in this code that would require bash.

– Kusalananda
9 hours ago






I can not reproduce your issue using /bin/sh on a BSD system. There is nothing in this code that would require bash.

– Kusalananda
9 hours ago














My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now. The example indeed does not require Bash.

– Steiner
9 hours ago





My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now. The example indeed does not require Bash.

– Steiner
9 hours ago













@Steiner In fact, it would have the same issue in bash.

– Kusalananda
9 hours ago





@Steiner In fact, it would have the same issue in bash.

– Kusalananda
9 hours ago




3




3





There is no Bourne shell on the BSDs. Nor is the Bourne shell standard. Do not conflate the Bourne shell with a POSIX-conformant sh.

– JdeBP
9 hours ago





There is no Bourne shell on the BSDs. Nor is the Bourne shell standard. Do not conflate the Bourne shell with a POSIX-conformant sh.

– JdeBP
9 hours ago










3 Answers
3






active

oldest

votes


















3














You could use a here document:



while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done << EOF
$MULTILINE
EOF
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


Depending on the sh implementation, here-documents are implemented either as a deleted temporary file where the shell has stored the expansion of the variable followed by newline beforehand, or a pipe to which the shell feeds the expansion of the variable followed by newline. But in either case, except in the original Bourne shell (a shell that is no longer in use these days and is not a POSIX compliant shell), the command being redirected is not run in a subshell (as POSIX requires).



or you could use split+glob:



IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


But beware it skips empty lines.






share|improve this answer

























  • Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

    – Steiner
    4 hours ago











  • @Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

    – Kusalananda
    2 hours ago



















2














You would read directly from the file without the pipeline. This avoids running the while loop in a subshell, which allows you to see the changed value of $SOMEVALUE after the loop.



SOMEVAR="original value"

while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done <"$SOMEFILE"

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"


If you insist on having your $MULTILINE variable, then write that to a file and read it from there:



tmpfile=$(mktemp)
printf '%sn' "$MULTILINE" >"$tmpfile"

while ...; do
...
done <"$tmpfile"
rm "$tmpfile"


Also related:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

An answer to the above linked question also suggests writing your program in such a way that all uses of $SOMEVAR occurs within the subshell at the end of the pipeline:



MULTILINE=$(cat "$SOMEFILE")
SOMEVAR="original value"

printf '%sn' "$MULTILINE" |
while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"



Also possibly related:



  • Why is using a shell loop to process text considered bad practice?

Other questions that may be of interest:



  • Why is printf better than echo?

  • Are there naming conventions for variables in shell scripts?





share|improve this answer

























  • Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

    – Steiner
    9 hours ago











  • @Steiner See update answer. There's not much else you could do without using features of specific shells.

    – Kusalananda
    9 hours ago











  • Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

    – Steiner
    9 hours ago











  • @Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

    – Kusalananda
    2 hours ago


















0














It works for me :



$ cat bin/test
#! /bin/sh
SOMEFILE=$1
MULTILINE="`cat $SOMEFILE`"
SOMEVAR="blah"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
echo "this is a single line: $SINGLELINE"
echo "but accessing this var fails: $SOMEVAR"
done


and



$ bin/test bin/test
this is a single line: #! /bin/sh
but accessing this var fails: blah
this is a single line: SOMEFILE=$1
but accessing this var fails: blah
this is a single line: MULTILINE="`cat $SOMEFILE`"
but accessing this var fails: blah
this is a single line: SOMEVAR="blah"
but accessing this var fails: blah
this is a single line:
but accessing this var fails: blah
this is a single line: echo "$MULTILINE" | while IFS= read -r SINGLELINE
but accessing this var fails: blah
this is a single line: do
but accessing this var fails: blah
this is a single line: echo "this is a single line: $SINGLELINE"
but accessing this var fails: blah
this is a single line: echo "but accessing this var fails: $SOMEVAR"
but accessing this var fails: blah
this is a single line: done
but accessing this var fails: blah





share|improve this answer








New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

    – Steiner
    9 hours ago











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509714%2fiterate-through-multiline-string-line-by-line%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You could use a here document:



while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done << EOF
$MULTILINE
EOF
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


Depending on the sh implementation, here-documents are implemented either as a deleted temporary file where the shell has stored the expansion of the variable followed by newline beforehand, or a pipe to which the shell feeds the expansion of the variable followed by newline. But in either case, except in the original Bourne shell (a shell that is no longer in use these days and is not a POSIX compliant shell), the command being redirected is not run in a subshell (as POSIX requires).



or you could use split+glob:



IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


But beware it skips empty lines.






share|improve this answer

























  • Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

    – Steiner
    4 hours ago











  • @Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

    – Kusalananda
    2 hours ago
















3














You could use a here document:



while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done << EOF
$MULTILINE
EOF
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


Depending on the sh implementation, here-documents are implemented either as a deleted temporary file where the shell has stored the expansion of the variable followed by newline beforehand, or a pipe to which the shell feeds the expansion of the variable followed by newline. But in either case, except in the original Bourne shell (a shell that is no longer in use these days and is not a POSIX compliant shell), the command being redirected is not run in a subshell (as POSIX requires).



or you could use split+glob:



IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


But beware it skips empty lines.






share|improve this answer

























  • Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

    – Steiner
    4 hours ago











  • @Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

    – Kusalananda
    2 hours ago














3












3








3







You could use a here document:



while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done << EOF
$MULTILINE
EOF
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


Depending on the sh implementation, here-documents are implemented either as a deleted temporary file where the shell has stored the expansion of the variable followed by newline beforehand, or a pipe to which the shell feeds the expansion of the variable followed by newline. But in either case, except in the original Bourne shell (a shell that is no longer in use these days and is not a POSIX compliant shell), the command being redirected is not run in a subshell (as POSIX requires).



or you could use split+glob:



IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


But beware it skips empty lines.






share|improve this answer















You could use a here document:



while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done << EOF
$MULTILINE
EOF
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


Depending on the sh implementation, here-documents are implemented either as a deleted temporary file where the shell has stored the expansion of the variable followed by newline beforehand, or a pipe to which the shell feeds the expansion of the variable followed by newline. But in either case, except in the original Bourne shell (a shell that is no longer in use these days and is not a POSIX compliant shell), the command being redirected is not run in a subshell (as POSIX requires).



or you could use split+glob:



IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
SOMEVAR="updated value"
printf '%sn' "this is a single line: $SINGLELINE"
printf '%sn' "SOMEVAR is now: $SOMEVAR"
done
printf '%sn' "Final SOMEVAL is still $SOMEVAR"


But beware it skips empty lines.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 7 hours ago









Stéphane ChazelasStéphane Chazelas

312k57590946




312k57590946












  • Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

    – Steiner
    4 hours ago











  • @Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

    – Kusalananda
    2 hours ago


















  • Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

    – Steiner
    4 hours ago











  • @Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

    – Kusalananda
    2 hours ago

















Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

– Steiner
4 hours ago





Both solutions worked well for me with /bin/sh. I think this solution is better than the one Kusalananda provided, though his answer is very informative too. I particularly like the for-loop solution since it probably is faster because it does not use read which is very slow. Thanks!

– Steiner
4 hours ago













@Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

– Kusalananda
2 hours ago






@Steiner (Just had to quickly double check so that I didn't have a for-loop solution, which I fortunately did not have)

– Kusalananda
2 hours ago














2














You would read directly from the file without the pipeline. This avoids running the while loop in a subshell, which allows you to see the changed value of $SOMEVALUE after the loop.



SOMEVAR="original value"

while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done <"$SOMEFILE"

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"


If you insist on having your $MULTILINE variable, then write that to a file and read it from there:



tmpfile=$(mktemp)
printf '%sn' "$MULTILINE" >"$tmpfile"

while ...; do
...
done <"$tmpfile"
rm "$tmpfile"


Also related:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

An answer to the above linked question also suggests writing your program in such a way that all uses of $SOMEVAR occurs within the subshell at the end of the pipeline:



MULTILINE=$(cat "$SOMEFILE")
SOMEVAR="original value"

printf '%sn' "$MULTILINE" |
while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"



Also possibly related:



  • Why is using a shell loop to process text considered bad practice?

Other questions that may be of interest:



  • Why is printf better than echo?

  • Are there naming conventions for variables in shell scripts?





share|improve this answer

























  • Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

    – Steiner
    9 hours ago











  • @Steiner See update answer. There's not much else you could do without using features of specific shells.

    – Kusalananda
    9 hours ago











  • Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

    – Steiner
    9 hours ago











  • @Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

    – Kusalananda
    2 hours ago















2














You would read directly from the file without the pipeline. This avoids running the while loop in a subshell, which allows you to see the changed value of $SOMEVALUE after the loop.



SOMEVAR="original value"

while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done <"$SOMEFILE"

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"


If you insist on having your $MULTILINE variable, then write that to a file and read it from there:



tmpfile=$(mktemp)
printf '%sn' "$MULTILINE" >"$tmpfile"

while ...; do
...
done <"$tmpfile"
rm "$tmpfile"


Also related:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

An answer to the above linked question also suggests writing your program in such a way that all uses of $SOMEVAR occurs within the subshell at the end of the pipeline:



MULTILINE=$(cat "$SOMEFILE")
SOMEVAR="original value"

printf '%sn' "$MULTILINE" |
while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"



Also possibly related:



  • Why is using a shell loop to process text considered bad practice?

Other questions that may be of interest:



  • Why is printf better than echo?

  • Are there naming conventions for variables in shell scripts?





share|improve this answer

























  • Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

    – Steiner
    9 hours ago











  • @Steiner See update answer. There's not much else you could do without using features of specific shells.

    – Kusalananda
    9 hours ago











  • Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

    – Steiner
    9 hours ago











  • @Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

    – Kusalananda
    2 hours ago













2












2








2







You would read directly from the file without the pipeline. This avoids running the while loop in a subshell, which allows you to see the changed value of $SOMEVALUE after the loop.



SOMEVAR="original value"

while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done <"$SOMEFILE"

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"


If you insist on having your $MULTILINE variable, then write that to a file and read it from there:



tmpfile=$(mktemp)
printf '%sn' "$MULTILINE" >"$tmpfile"

while ...; do
...
done <"$tmpfile"
rm "$tmpfile"


Also related:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

An answer to the above linked question also suggests writing your program in such a way that all uses of $SOMEVAR occurs within the subshell at the end of the pipeline:



MULTILINE=$(cat "$SOMEFILE")
SOMEVAR="original value"

printf '%sn' "$MULTILINE" |
while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"



Also possibly related:



  • Why is using a shell loop to process text considered bad practice?

Other questions that may be of interest:



  • Why is printf better than echo?

  • Are there naming conventions for variables in shell scripts?





share|improve this answer















You would read directly from the file without the pipeline. This avoids running the while loop in a subshell, which allows you to see the changed value of $SOMEVALUE after the loop.



SOMEVAR="original value"

while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done <"$SOMEFILE"

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"


If you insist on having your $MULTILINE variable, then write that to a file and read it from there:



tmpfile=$(mktemp)
printf '%sn' "$MULTILINE" >"$tmpfile"

while ...; do
...
done <"$tmpfile"
rm "$tmpfile"


Also related:



  • Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

An answer to the above linked question also suggests writing your program in such a way that all uses of $SOMEVAR occurs within the subshell at the end of the pipeline:



MULTILINE=$(cat "$SOMEFILE")
SOMEVAR="original value"

printf '%sn' "$MULTILINE" |
while IFS= read -r SINGLELINE
do
SOMEVAR="updated value"
printf 'this is a single line: %sn' "$SINGLELINE"
printf 'SOMEVAR is now: %sn' "$SOMEVAR"
done

printf 'Final SOMEVAR is: %sn' "$SOMEVAR"



Also possibly related:



  • Why is using a shell loop to process text considered bad practice?

Other questions that may be of interest:



  • Why is printf better than echo?

  • Are there naming conventions for variables in shell scripts?






share|improve this answer














share|improve this answer



share|improve this answer








edited 9 hours ago

























answered 9 hours ago









KusalanandaKusalananda

139k17259429




139k17259429












  • Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

    – Steiner
    9 hours ago











  • @Steiner See update answer. There's not much else you could do without using features of specific shells.

    – Kusalananda
    9 hours ago











  • Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

    – Steiner
    9 hours ago











  • @Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

    – Kusalananda
    2 hours ago

















  • Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

    – Steiner
    9 hours ago











  • @Steiner See update answer. There's not much else you could do without using features of specific shells.

    – Kusalananda
    9 hours ago











  • Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

    – Steiner
    9 hours ago











  • @Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

    – Kusalananda
    2 hours ago
















Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

– Steiner
9 hours ago





Indeed this works, but requires a file to be fed to the while-loop. My actual code is more complex, and requires a modified multiline string to be fed to the while-loop. Your solution to write this multiline string to a temporary file works, but is there a solution that does not require writing a temporary file?

– Steiner
9 hours ago













@Steiner See update answer. There's not much else you could do without using features of specific shells.

– Kusalananda
9 hours ago





@Steiner See update answer. There's not much else you could do without using features of specific shells.

– Kusalananda
9 hours ago













Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

– Steiner
9 hours ago





Thank you that answers my question. The suggestion at the end and the links provided are very helpful too!

– Steiner
9 hours ago













@Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

– Kusalananda
2 hours ago





@Steiner ... except for using a here-document. Well, there's a thing I didn't think of :-)

– Kusalananda
2 hours ago











0














It works for me :



$ cat bin/test
#! /bin/sh
SOMEFILE=$1
MULTILINE="`cat $SOMEFILE`"
SOMEVAR="blah"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
echo "this is a single line: $SINGLELINE"
echo "but accessing this var fails: $SOMEVAR"
done


and



$ bin/test bin/test
this is a single line: #! /bin/sh
but accessing this var fails: blah
this is a single line: SOMEFILE=$1
but accessing this var fails: blah
this is a single line: MULTILINE="`cat $SOMEFILE`"
but accessing this var fails: blah
this is a single line: SOMEVAR="blah"
but accessing this var fails: blah
this is a single line:
but accessing this var fails: blah
this is a single line: echo "$MULTILINE" | while IFS= read -r SINGLELINE
but accessing this var fails: blah
this is a single line: do
but accessing this var fails: blah
this is a single line: echo "this is a single line: $SINGLELINE"
but accessing this var fails: blah
this is a single line: echo "but accessing this var fails: $SOMEVAR"
but accessing this var fails: blah
this is a single line: done
but accessing this var fails: blah





share|improve this answer








New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

    – Steiner
    9 hours ago















0














It works for me :



$ cat bin/test
#! /bin/sh
SOMEFILE=$1
MULTILINE="`cat $SOMEFILE`"
SOMEVAR="blah"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
echo "this is a single line: $SINGLELINE"
echo "but accessing this var fails: $SOMEVAR"
done


and



$ bin/test bin/test
this is a single line: #! /bin/sh
but accessing this var fails: blah
this is a single line: SOMEFILE=$1
but accessing this var fails: blah
this is a single line: MULTILINE="`cat $SOMEFILE`"
but accessing this var fails: blah
this is a single line: SOMEVAR="blah"
but accessing this var fails: blah
this is a single line:
but accessing this var fails: blah
this is a single line: echo "$MULTILINE" | while IFS= read -r SINGLELINE
but accessing this var fails: blah
this is a single line: do
but accessing this var fails: blah
this is a single line: echo "this is a single line: $SINGLELINE"
but accessing this var fails: blah
this is a single line: echo "but accessing this var fails: $SOMEVAR"
but accessing this var fails: blah
this is a single line: done
but accessing this var fails: blah





share|improve this answer








New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

    – Steiner
    9 hours ago













0












0








0







It works for me :



$ cat bin/test
#! /bin/sh
SOMEFILE=$1
MULTILINE="`cat $SOMEFILE`"
SOMEVAR="blah"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
echo "this is a single line: $SINGLELINE"
echo "but accessing this var fails: $SOMEVAR"
done


and



$ bin/test bin/test
this is a single line: #! /bin/sh
but accessing this var fails: blah
this is a single line: SOMEFILE=$1
but accessing this var fails: blah
this is a single line: MULTILINE="`cat $SOMEFILE`"
but accessing this var fails: blah
this is a single line: SOMEVAR="blah"
but accessing this var fails: blah
this is a single line:
but accessing this var fails: blah
this is a single line: echo "$MULTILINE" | while IFS= read -r SINGLELINE
but accessing this var fails: blah
this is a single line: do
but accessing this var fails: blah
this is a single line: echo "this is a single line: $SINGLELINE"
but accessing this var fails: blah
this is a single line: echo "but accessing this var fails: $SOMEVAR"
but accessing this var fails: blah
this is a single line: done
but accessing this var fails: blah





share|improve this answer








New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










It works for me :



$ cat bin/test
#! /bin/sh
SOMEFILE=$1
MULTILINE="`cat $SOMEFILE`"
SOMEVAR="blah"

echo "$MULTILINE" | while IFS= read -r SINGLELINE
do
echo "this is a single line: $SINGLELINE"
echo "but accessing this var fails: $SOMEVAR"
done


and



$ bin/test bin/test
this is a single line: #! /bin/sh
but accessing this var fails: blah
this is a single line: SOMEFILE=$1
but accessing this var fails: blah
this is a single line: MULTILINE="`cat $SOMEFILE`"
but accessing this var fails: blah
this is a single line: SOMEVAR="blah"
but accessing this var fails: blah
this is a single line:
but accessing this var fails: blah
this is a single line: echo "$MULTILINE" | while IFS= read -r SINGLELINE
but accessing this var fails: blah
this is a single line: do
but accessing this var fails: blah
this is a single line: echo "this is a single line: $SINGLELINE"
but accessing this var fails: blah
this is a single line: echo "but accessing this var fails: $SOMEVAR"
but accessing this var fails: blah
this is a single line: done
but accessing this var fails: blah






share|improve this answer








New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 9 hours ago









RedocTsujRedocTsuj

1




1




New contributor




RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






RedocTsuj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

    – Steiner
    9 hours ago

















  • My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

    – Steiner
    9 hours ago
















My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

– Steiner
9 hours ago





My apologies. In trying to create a minimized sample of the actual issue, i made some errors. I updated the code sample now, and it should reflect the problem correctly now.

– Steiner
9 hours ago

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509714%2fiterate-through-multiline-string-line-by-line%23new-answer', 'question_page');

);

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







Popular posts from this blog

Era Viking Índice Início da Era Viquingue | Cotidiano | Sociedade | Língua | Religião | A arte | As primeiras cidades | As viagens dos viquingues | Viquingues do Oeste e Leste | Fim da Era Viquingue | Fontes históricas | Referências Bibliografia | Ligações externas | Menu de navegação«Sverige då!»«Handel I vikingetid»«O que é Nórdico Antigo»Mito, magia e religião na volsunga saga Um olhar sobre a trajetória mítica do herói sigurd«Bonden var den verklige vikingen»«Vikingatiden»«Vikingatiden»«Vinland»«Guerreiras de Óðinn: As Valkyrjor na Mitologia Viking»1519-9053«Esculpindo símbolos e seres: A arte viking em pedras rúnicas»1679-9313Historia - Tema: VikingarnaAventura e Magia no Mundo das Sagas IslandesasEra Vikinge

What's the metal clinking sound at the end of credits in Avengers: Endgame?What makes Thanos so strong in Avengers: Endgame?Who is the character that appears at the end of Endgame?What happens to Mjolnir (Thor's hammer) at the end of Endgame?The People's Ages in Avengers: EndgameWhat did Nebula do in Avengers: Endgame?Messing with time in the Avengers: Endgame climaxAvengers: Endgame timelineWhat are the time-travel rules in Avengers Endgame?Why use this song in Avengers: Endgame Opening Logo Sequence?Peggy's age in Avengers Endgame

Are there legal definitions of ethnicities/races? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Legal definitions in the United StatesAre there truly legal limits on US interest rates?Are gender identity and sexual orientation federally protected?Why is there an apparent legal bias against digital services?What limits are there to the powers of individual judges in the United States legal system?Are women only scholarships legal under Irish / EU law?Is the term “race” defined by Public Law enacted by Congress of the United StatesIs there a legal definition of race in the US?Neighbors are spying for landlord on Renters is it legal?Are Protected Classes Bi-directional?