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
.
bash
? (add to your question) – ctrl-alt-delor Mar 31 '19 at 10:54/bin/sh
on a BSD system. There is nothing in this code that would requirebash
. – Kusalananda Mar 31 '19 at 10:57bash
. – Kusalananda Mar 31 '19 at 11:21sh
. – JdeBP Mar 31 '19 at 11:29