gitgit-configgit-alias

Setting a Git alias which uses TR to translate backslash into forward slash


I am trying to set up a Git alias which has to convert a backslash to a forward slash to pass it later to filter-branch command (the need arises since I use Posh and will pass DOS based file path as a parameter).

So I have this alias:

echo-test = "!f() { path=$(echo $1 | tr '\\' '/'); echo $path; }; f"

But I get this error:

tr: warning: an unescaped backslash at end of string is not portable

I tried writing tr '\\\\' '/' inside, thinking that Git simply escapes the \ and bash gets a single \, but I get the same error.

Any ideas?


Solution

  • You need to write 8 backslashes here, because the string will be unescaped three times.

    echo-test = "!f() { path=$(echo $1 | tr '\\\\\\\\' '/'); echo $path; }; f"
    
    1. The first doubling is due to tr as OP have already used.
    2. The second doubling is due to the format of .gitconfig

      Inside double quotes, double quote " and backslash \ characters must be escaped: use \" for " and \\ for \.

    3. The third doubling is due to executing the command using sh -c "…".