variable value not retained after while loop

49 Views Asked by At

I am writing a loop which accumulates strings in a variable def, reading from a heredoc. I can't see the value of variable after the loop exits.

#!/usr/bin/sh

def=""
while read x; do
   def="$def -D$x"
   echo $def
done <<-COMMANDS_GREEN
hello="hello green world"
host="greenmen.com"
port=80
COMMANDS_GREEN

################### exit loop ########################
echo definition: $def

As the script output, inside the loop all. The definition: which is outside the loop doesn't show anything:

-Dhello="hello green world"
-Dhello="hello green world" -Dhost="greenmen.com"
-Dhello="hello green world" -Dhost="greenmen.com" -Dport=80
################### exit loop ########################
definition:

I am writing this under Solaris 10, not bash

1

There are 1 best solutions below

0
armagedescu On

Changed to ksh, it solved the problem

#!/usr/bin/ksh

thanks to @jhnc and @Gilles Quénot for info provided in comments