bashgitgit-bashhusky

Husky pre-commit hook and echo -e on Linux and Windows


I use the Husky pre-commit hook and I have code like this:

#!/usr/bin/env bash
. "$(dirname -- "$0")/_/husky.sh"

...

MISSING="$MISSING\nmissing docs for $path";

...

if [ ! -z "$MISSING" ]; then
    echo -e "$MISSING";
    false
fi

The problem is that on Linux echo -e makes it render -e in the terminal. But without it on Windows (in git bash) it doesn't render newlines.

What should I do to use echo -e for both real GNU/Linux and git bash on Windows?

I was investigating the files used by Husky and it has file .husky/_/husky.sh

that has this shebang:

#!/usr/bin/env sh

but changing it to bash makes no difference.


Solution

  • as @knittl wrote:

    This is exactly the reason why echo shouldn't be used if you require portable scripts. Use printf instead which is specified in POSIX and must behave in the defined way

    The printf command works perfectly but you need to add a newline at the end:

    if [ ! -z "$MISSING" ]; then
        printf "$MISSING\n";
        false
    fi