0

Goal

I want to take a line break separated file (path in $2), check the path exists and add it to folderList.

Current Code

I am not yet checking for file path correctness as I can't get concatenation to work.

folderList=""

cat $2 | while read line
do
   folderList="$folderList;$line"
   echo $line #echos the right content
done

echo $folderList

The output shoes that it echos line correctly but folderList is simply an empty line (not showing on Stack Overflow formatting).

foo
bar

Working Code Without File Path Checking

folderList="$(tr '\n' ';' < $2)"
Geesh_SO
  • 2,156
  • 5
  • 31
  • 58
  • 1
    Don't use pipeline otherwise your changes are being made in a subshell. Use `done < "$2"` – anubhava Aug 19 '19 at 14:05
  • See: [Can't use a variable out of while and pipe in bash](https://stackoverflow.com/q/6572411/3776858) and [How to pipe input to a Bash while loop and preserve variables after loop ends](https://stackoverflow.com/q/19570413/3776858) – Cyrus Aug 19 '19 at 14:06
  • Those comments hit the nail on the head and fixed it. Happy to have it closed for duplication. Thanks for the help. – Geesh_SO Aug 19 '19 at 14:27
  • This might help, too: [Bash don't capture ssh errors](https://stackoverflow.com/q/57401200/3776858) – Cyrus Aug 19 '19 at 14:28

0 Answers0