Skip to main content
1 of 3
Peter.O
  • 33.8k
  • 32
  • 120
  • 167

If you want to concatenate a list of files. Youc can check each one as follows:

printf "%s\n" abc > file1  
printf "%s"   def > file2  # no trailing newline
printf "%s\n" abc > file3
printf "%s"   def > file4  # no trailing newline

tmp="$(mktemp)"; echo >$tmp
cmd=cat
for file in file1 file2 file3 file4; do
    [[ $(tail -c1 $file | hexdump -ve '1/1 "%02x"') == 0a ]] && nl= || nl=" $tmp"
    cmd="$cmd \"$file\"$nl"
done
eval "$cmd"

The concatenated result is:

abc
def
abc
def
Peter.O
  • 33.8k
  • 32
  • 120
  • 167