Well... before the shell executes the for loop, it substitutes the output of the env command so it truly executes something like the following
Code:
for i in LESS=-M -I LESSOPEN=lessopen.sh %s LESSCLOSE=lessclose.sh %s %s and so on ...
do
echo $i
done
that is it considers a long list of strings (fields) separated by blank spaces and execute the command inside the loop one by one. To prevent this behavior (splitting a long string by blank spaces) you have to modify the value of the Input Field Separator, which is stored in the shell variable IFS. The correct way is:
Code:
# Store the original IFS
OLD_IFS=${IFS}
# Set IFS to 'newline'
IFS=$(echo)
# Now execute the loop splitting the string by newlines instead of blank spaces
for i in $(env)
do
echo $i
done
# Restore the original IFS
IFS=${OLD_IFS}
Note that the syntax $(command) is equivalent to `command`. See man bash, section "Command substitution" for details.