# escape

```
# escape=\ (backslash)
```

或者

```
# escape=` (backtick)
```

**escape指令**设置用于对**Dockerfile**中的字符进行转义的字符。如果未指定，则默认转义字符&#x4E3A;**\\**

转义字符用于转义一行中的字符和转义换行符。这允许**Dockerfile**指令跨越多行。请注意，无论**Dockerfile**中是否包含转义**解析器指令**，都不会在**RUN命令**中执行转义，除非在行末。

在**Windows**上将转义符设置成其他字符特别有用，其&#x4E2D;**\\**&#x662F;目录路径分隔符 , **Windows PowerShell**一致。

考虑以下示例，该示例在**Windows**上将以非显而易见的方式失败。第二行末尾的第二&#x4E2A;**\\**&#x5C06;被解释为换行符的转义符，而不是第一&#x884C;**\\**&#x7684;转义目标。同样，假设第三行末尾&#x7684;**\\**&#x5B9E;际上是作为指令处理的，则将其视为行的延续。该**dockerfile**的结果是第二和第三行被视为一条指令：

```
FROM microsoft/nanoserver
COPY testfile.txt c:\ #这里的\会被当成行连续符
RUN dir c:\
```

结果：

```
PS C:\John> docker build -t cmd .
Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/2 : COPY testfile.txt c:\RUN dir c:
GetFileAttributesEx c:RUN: The system cannot find the file specified.
PS C:\John>
```

上述解决方案之一是&#x5C06;**/**&#x7528;作**COPY指令**和**dir**的目标路径。但是，此语法容器让人产生困惑，因为**Windows**上的路径并不自然，而最糟糕的是，由于**Windows**上的所有命令都不支&#x6301;**/**&#x4F5C;为路径分隔符，因此容易出错。

通过添加**escape**解析器指令，以下**Dockerfile**会通过在**Windows**上使用平台指定的语义（也就&#x662F;**\\**)来做为文件路径分隔符使用而成功执行：

```
PS C:\John> docker build -t succeeds --no-cache=true .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/3 : COPY testfile.txt c:\
 ---> 96655de338de
Removing intermediate container 4db9acbb1682
Step 3/3 : RUN dir c:\
 ---> Running in a2c157f842f5
 Volume in drive C has no label.
 Volume Serial Number is 7E6D-E0F7

 Directory of c:\

10/05/2016  05:04 PM             1,894 License.txt
10/05/2016  02:22 PM    <DIR>          Program Files
10/05/2016  02:14 PM    <DIR>          Program Files (x86)
10/28/2016  11:18 AM                62 testfile.txt
10/28/2016  11:20 AM    <DIR>          Users
10/28/2016  11:20 AM    <DIR>          Windows
           2 File(s)          1,956 bytes
           4 Dir(s)  21,259,096,064 bytes free
 ---> 01c7f3bef04f
Removing intermediate container a2c157f842f5
Successfully built 01c7f3bef04f
PS C:\John>
```
