How to Handle Path Separators in Windows Scripts in GitHub Actions
Windows accepts forward slashes in most tools, so use them for paths that must also work on Linux.
Most cross-platform tools and PowerShell accept / on Windows, so prefer forward slashes. When a native command needs backslashes, branch on runner.os.
Steps
- Use forward slashes in paths handled by node, python, git, and PowerShell.
- Branch on
${{ runner.os }}when a step truly needs OS-specific separators. - Reference
${{ github.workspace }}rather than hardcoding a drive path.
Workflow
.github/workflows/ci.yml
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Forward slashes work on Windows
shell: pwsh
run: Get-Content "src/config/app.json"
- name: OS-specific path when required
shell: pwsh
run: |
if ($env:RUNNER_OS -eq "Windows") {
& ".\tools\win\pack.exe"
} else {
& "./tools/linux/pack"
}Gotchas
- In bash on Windows (Git Bash), backslashes are escape characters, so prefer forward slashes there.
- Use
${{ github.workspace }}instead ofC:\paths so the workflow stays portable.
Related guides
How to Run Tests on a Windows and Linux Matrix in GitHub ActionsRun the same test job on Windows and Linux in GitHub Actions with an os matrix driving runs-on, and a portabl…
How to Handle CRLF Line Endings on Windows in GitHub ActionsStop CRLF and LF line-ending churn on a windows-latest runner in GitHub Actions by configuring git core.autoc…