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 related question http://stackoverflow.com/questions/124167/bash-variable-scope
#!/bin/bash
CP="AAA"
func() {
ls -1 | while read JAR
do
if [ ! -z "$CP" ]; then
CP=${CP}':'
fi
CP=${CP}${JAR}
done
echo $CP # <-- prints AAA
}
func
My question is, since I can't figure out which element will be the last one, how can the value be saved.
Do I actually have to save the current value (repeatedly in the loop) to a file?
EDIT:
A colleague came up with this command sequence which works well
ls | xargs echo|tr ' ' :