2 added 643 characters in body
source | link

You could use a here document:

while IFS= read -r SINGLELINE
do
  SOMEVAR="updated value"
  echoprintf '%s\n' "this is a single line: ${SINGLELINE}"
  echoprintf '%s\n' "SOMEVAR is now: ${SOMEVAR}"
done << EOF
$MULTILINE
EOF
printf '%s\n' "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"
  echoprintf '%s\n' "this is a single line: ${SINGLELINE}"
  echoprintf '%s\n' "SOMEVAR is now: ${SOMEVAR}"
done
printf '%s\n' "Final SOMEVAL is still $SOMEVAR"

But beware it skips empty lines.

You could use a here document:

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

or split+glob:

IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
  SOMEVAR="updated value"
  echo "this is a single line: ${SINGLELINE}"
  echo "SOMEVAR is now: ${SOMEVAR}"
done

But beware it skips empty lines.

You could use a here document:

while IFS= read -r SINGLELINE
do
  SOMEVAR="updated value"
  printf '%s\n' "this is a single line: ${SINGLELINE}"
  printf '%s\n' "SOMEVAR is now: ${SOMEVAR}"
done << EOF
$MULTILINE
EOF
printf '%s\n' "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 '%s\n' "this is a single line: ${SINGLELINE}"
  printf '%s\n' "SOMEVAR is now: ${SOMEVAR}"
done
printf '%s\n' "Final SOMEVAL is still $SOMEVAR"

But beware it skips empty lines.

1
source | link

You could use a here document:

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

or split+glob:

IFS='
' # split on newline only
set -o noglob
for SINGLELINE in $MULTILINE
do
  SOMEVAR="updated value"
  echo "this is a single line: ${SINGLELINE}"
  echo "SOMEVAR is now: ${SOMEVAR}"
done

But beware it skips empty lines.