Bash – How to Force Newlines with Cat Wildcard Printing

bashcatnewlineswildcards

I want to use cat with wildcards in bash in order to print multiple small files (every file is one sentence) to standard output. However, the separate file contents are not separated by a newline, which I'd like to for ease of reading.

How can I add a file delimiter of some sort to this command?

Best Answer

Define a shell function which outputs an end of line after every file and use it instead of cat:

endlcat() {
  for file in "$@"; do
    cat -- "$file"
    echo
  done
}

then you can use endlcat *.

The for loop loops over all provided arguments ($@) which are already escaped by the shell when you use wildcards like *. The -- is required to not choke on file names starting with a dash. Finally the echo outputs a newline.

Related Question