Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ensure Trailing Newline: ensures that the given plaintext file ends with a newline character, appending in-place only if it is missing.
#!/usr/bin/env bash -e
#
# Ensure Trailing Newline: ensures that the given plaintext file ends with a
# newline character, appending in-place only if it is missing.
#
# Portable on POSIX-based or POSIX-compatible systems, as it uses POSIX-standard
# invocations of `wc`, `tail`, and `echo`.
#
# Copyright 2018 Ry Biesemeyer
#
# Made available under the [MIT license](https://opensource.org/licenses/MIT)
TARGET="${1:?USAGE: $(basename $0) <filename>}"
if [ $(tail -c1 "${TARGET}" | wc -l) -eq 0 ]; then
echo >> "${TARGET}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.