This bash script concatenates the names for jar files to a classpath (variable CP), in the while loop the value is correct but is lost in the subshell as descibed in this re
Implicit subshells are confusing; always make them explicit by using parentheses. To solve your problem, just move the echo inside the subshell.
#!/bin/bash CP="AAA" func() { ls -1 | ( while read JAR do if [ ! -z "$CP" ]; then CP=${CP}':' fi CP=${CP}${JAR} done echo $CP ) } func