Gabrielius
Gabrielius

Reputation: 1056

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?

Upvotes: 2

Views: 4404

Answers (2)

Aquarius Power
Aquarius Power

Reputation: 3985

I know the question is about tr but this is an equivalent alternative.

If you could write the |tr ... command,
it is much more clear (less confuse) to use |sed -r 's"[\]"/"g'.

I hope I can always avoid using \\\\\\\\\\\... I will never get used to it.

Upvotes: 0

kennytm
kennytm

Reputation: 523334

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 "…".

Upvotes: 3

Related Questions