retrodev
retrodev

Reputation: 2393

Appending to file in bash loop is adding ^M

I'm running a Bash script that loops through the files in a directory and appends them all to a single file.

However, I find that ^M is being added to the end of every line.

The original files do not contain this escape character and manually appending the files on the command line does not insert the character.

I don't know if it matters, but I'm using eval to construct and then retrieve the directory names as below:

Construct directory names:

declare ${schema}_${type}_${subtype}="$(eval echo \$${schema}_${type}_${subtype}) $(echo $file | egrep -v "$excluded_types" | grep $schema/$type/$subtype)"

Retrieve directory names:

for file in $(eval echo \$${schema}_${type}_${subtype})
do
  echo -e "\t\t\t$file"
  echo -e "\t\t\t$file\n" >> $log_file
  cat $file >> $output_file
done

Upvotes: 1

Views: 1386

Answers (2)

Nicholas Wilson
Nicholas Wilson

Reputation: 9685

For the record and for googlers, this arises most commonly from Windows line endings. Use nix line endings for your script.

Upvotes: 0

kurumi
kurumi

Reputation: 25599

you can always do a dos2unix on your files before processing.

Upvotes: 2

Related Questions