Forward slash vs backward slash for file path in git bash

后端 未结 4 1302
孤街浪徒
孤街浪徒 2021-01-14 08:36

I am running these two commands in Git bash.

Why they behave differently? Aren\'t they supposed to do the same thing or am I missing something?

4条回答
  •  臣服心动
    2021-01-14 09:21

    Bash treats backslash as an escape character, meaning that the symbol following it is interpreted literally, and the backslash itself is dropped.

    $ echo $HOME
    /home/user
    $ echo \$HOME
    $HOME
    

    Under Windows, where backslash serves as a path separator, this causes some inconvenience. Fortunately, inside single quotes a backslash character loses its special meaning and is treated literally (as any other character, except a single quote):

    $ echo '\$HOME'
    \$HOME
    

    Therefore, if you are going to copy&paste a Windows path into Git bash, put it inside single quotes:

    git diff > 'D:\Patches\afterWGComment.txt'
    

提交回复
热议问题