Do what you always (should) do:
- separate concerns,
- avoid globals,
- document your code,
- be readable,
- maybe be POSIXy.
(Yes, I added a bit more "good practice" ingredients to the soup than absolutely necessary ;))
So my favorite "knee-jerk reaction" to problems with invisible subshell is to use function:
#!/bin/sh
longest() {
#
# Print length and body of the longest line in STDIN
#
local cur_ln # current line
local cur_sz # current size (line length)
local max_sz # greatest size so far
local winner # longest string so far
max_sz=0
while read -r cur_ln
do
cur_sz=${#cur_ln}
if test "$cur_sz" -gt "$max_sz";
then
max_sz=$cur_sz
winner=$cur_ln
fi
done
echo "$max_sz" : "$winner"
}
find /usr/share/zoneinfo | longest
# ok, if you really wish to use globals, here you go ;)
LONGEST_CNT=0
LONGEST_CNT=$(
find /usr/share/zoneinfo \
| longest \
| cut -d: -f1 \
| xargs echo\
)
echo "LONGEST_CNT='$LONGEST_CNT'"
Aside from avoiding the subshell annoyance, it gives you perfect place to document the code, and sort-of adds namespacing: notice that inside function you can use much shorter and simpler variable names without losing any of readability.