#!/bin/bash
Dir=""
while read line
do echo "Record is :$line"
Dir+="Dir,$line"
done < dir.csv
echo $Dir
where dir.csv is an input file here which includes following data:
/example/people/
/book/
/book/english/
I would like to append the values of the rows into one variable like
/example/people/,/book/,/book/english/
Is there any easy way to achieve this through shell script? above script is showing only the last value ex:/book/english/
tr '\n' ',' < dir.csvDir+="Dir,$line"but you should be usingDir="$Dir,$line"orDir="$Dir${Dir:+,}$line", where the more complex version avoids a leading comma. This works in any shell derived from the Bourne shell (sh,bash,zsh,dash,ksh, …). In Bash (and probably some other modern shells), you could useDir+="${Dir:+,}$line", where the${Dir:+,}part adds a comma only ifDiris set and non-empty — avoiding the leading comma again. See Shell parameter expansion.